您的位置:首页 > 数据库 > MySQL

mysql存储过程之一些杂碎知识

2017-11-01 22:08 393 查看
 XML Code 
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

1、在存储程序中使用非 SELECT SQL 语句(可以向表中添加/修改/删除数据)

create table t1(id int ,name varchar(20));

create procedure insert_data(in count int)

begin

    declare i int default 0;

    repeat 

        set i=i+1;

        insert into t1(id,name) values(i,concat('name-',i)); 

    until i=10

    end repeat;

end$$

2、把表中的数据读入本地变量(使用select-into语法结构)

create procedure use_select_into()

begin

    declare count int default 0;

    select count(1) into count from t1;

    select concat('==>',count);

end$$

create procedure use_select_into2()

begin

    declare row_id int default 0;   

    declare row_name varchar(20) default '';

    declare i int default 0;

    

    while i<=10 do 

        set i=i+1;

        select id,name into row_id,row_name from t1 where id = i;

        select concat('[',i,'] id=',row_id,' name=',row_name);

    end while;

end$$
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: