mysql存储过程实例

delimiter ||
-- after INSERT 触发器
drop TRIGGER if EXISTS insert1after2;
create trigger insert1after2 after INSERT on tab1 for each row
begin
insert into tab2(id, username) values(new.id,new.username);
end||

delimiter ||
-- 插入存储过程
drop procedure if exists insert1;
create procedure insert1()
begin
DECLARE i int;
set i = 0;
WHILE i<100 do
insert into tab1(id,username) values(i,CONCAT('username',i));
set i = i+1;
end while;
end||

delimiter ||
-- 查询存储过程
drop procedure if exists selecttab1;
create procedure selecttab1()
begin
DECLARE i int;
declare username VARCHAR(255);
-- 定义游标
declare selecttab1_cursor CURSOR for select id, username from tab1 ;
-- 打开游标
open selecttab1_cursor;
-- 首次填充数据到变量
FETCH next from selecttab1_cursor into i,username;
-- 循环游标
read_loop: LOOP
select i,username;
-- 再次填充数据到变量
FETCH next from selecttab1_cursor into i,username;
END LOOP;
-- 关闭游标
close selecttab1_cursor;
end||

 

 

 


delimiter ||
-- 存储过程动态执行sql
drop procedure if exists dongtaisql;
create procedure dongtaisql()
begin
declare v_sql varchar(500); -- 需要执行的SQL语句
DECLARE i int;
set i = 0;
WHILE i<10 do
set v_sql= concat('insert into tab2(id,username) values(',i,', \'username',i,'\');');
set @v_sql=v_sql; -- 注意很重要,将连成的字符串赋值给一个变量(可以之前没有定义,但要以@开头)
prepare stmt from @v_sql; -- 预处理需要执行的动态SQL,其中stmt是一个变量
EXECUTE stmt; -- 执行SQL语句
deallocate prepare stmt; -- 释放掉预处理段
set i = i+1;
end while;
end||

 

 1 drop procedure if exists insert_hd_id_gen;
 2 create procedure insert_hd_id_gen(table_name varchar(100))
 3     begin
 4         declare v_every_table_name varchar(100);
 5         declare v_auto_increment int;
 6         declare v_insert_sql_upper varchar(1000);
 7         declare v_insert_sql_lower varchar(1000);
 8         
 9         declare notfound int default 0;    #定义一个辅助变量用于判断
10         
11         #定义游标
12         declare selecttab1_cursor cursor for select distinct t.table_name, t.auto_increment from information_schema.tables t where t.table_schema = 'bpmapp53' and t.table_name = table_name;
13         declare continue handler for sqlstate '02000' set notfound=1;#定义declare continue handler,这个会根据上下文是否有结果判断是否执行set notfound = 1, 必须在游标定义后定义
14         
15         #打开游标
16         open selecttab1_cursor;
17             #首次填充数据到变量
18             fetch selecttab1_cursor into v_every_table_name, v_auto_increment;
19             while notfound<>1 do
20                 set v_insert_sql_upper = concat('insert into bpmapp53.hd_id_gen values (\'', upper(v_every_table_name), '\', ', v_auto_increment, ');');
21                 set @v_insert_sql_upper=v_insert_sql_upper;   #注意很重要,将连成成的字符串赋值给一个变量(可以之前没有定义,但要以@开头22                 prepare stmt_insert_sql_upper from @v_insert_sql_upper;  #预处理需要执行的动态sql,其中stmt是一个变量
23                 execute stmt_insert_sql_upper;      #执行sql语句
24                 deallocate prepare stmt_insert_sql_upper;     #释放掉预处理段
25                 
26                 set v_insert_sql_lower = concat('insert into bpmapp53.hd_id_gen values (\'', lower(v_every_table_name), '\', ', v_auto_increment, ');');
27                 set @v_insert_sql_lower=v_insert_sql_lower;   #注意很重要,将连成成的字符串赋值给一个变量(可以之前没有定义,但要以@开头28                 prepare stmt_insert_sql_lower from @v_insert_sql_lower;  #预处理需要执行的动态sql,其中stmt是一个变量
29                 execute stmt_insert_sql_lower;      #执行sql语句
30                 deallocate prepare stmt_insert_sql_lower;     #释放掉预处理段
31                 
32                 #再次填充数据到变量
33                 fetch selecttab1_cursor into v_every_table_name, v_auto_increment;
34             end while;
35         #关闭游标
36         close selecttab1_cursor;
37     end
38 
39 
40 drop procedure if exists alter_table_auto_increment;
41 create procedure alter_table_auto_increment(table_name varchar(100))
42     begin
43         declare v_every_table_name varchar(100);
44         declare v_alter_table varchar(1000);
45         
46         declare notfound int default 0;    #定义一个辅助变量用于判断
47         
48         #定义游标
49         declare selecttab1_cursor cursor for select distinct c.table_name from information_schema.columns c where c.column_name = 'id' and c.data_type = 'bigint' and c.column_key = 'pri' and c.table_schema = 'bpmapp53' and c.table_name = table_name;
50         declare continue handler for sqlstate '02000' set notfound=1;#定义declare continue handler,这个会根据上下文是否有结果判断是否执行set notfound = 1, 必须在游标定义后定义
51         
52         #打开游标
53         open selecttab1_cursor;
54             #首次填充数据到变量
55             fetch next from selecttab1_cursor into v_every_table_name;
56             while notfound<>1 do
57                 set v_alter_table= concat('alter table bpmapp53.', v_every_table_name, ' modify column id bigint(20) not null auto_increment first;');
58                 set @v_alter_table=v_alter_table;   #注意很重要,将连成成的字符串赋值给一个变量(可以之前没有定义,但要以@开头59                 prepare stmt_select_now_id from @v_alter_table;  #预处理需要执行的动态sql,其中stmt是一个变量
60                 execute stmt_select_now_id;      #执行sql语句
61                 deallocate prepare stmt_select_now_id;     #释放掉预处理段
62                 
63                 call insert_hd_id_gen(v_every_table_name);
64                 
65                 #再次填充数据到变量
66                 fetch next from selecttab1_cursor into v_every_table_name;
67             end while;
68         #关闭游标
69         close selecttab1_cursor;
70     end
71 
72 drop procedure if exists select_all_table;
73 create procedure select_all_table()
74     begin
75         declare table_name varchar(100);
76         declare notfound int default 0;    #定义一个辅助变量用于判断
77         
78         #定义游标
79         declare selecttab1_cursor cursor for select distinct t.table_name from information_schema.tables t  where t.table_schema = 'bpmapp53'  and t.auto_increment is null ;
80         declare continue handler for sqlstate '02000' set notfound=1;#定义declare continue handler,这个会根据上下文是否有结果判断是否执行set notfound = 1, 必须在游标定义后定义
81         
82         #清空hd_id_gen表数据
83         delete from bpmapp53.hd_id_gen;
84         
85         #打开游标
86         open selecttab1_cursor;
87             #首次填充数据到变量
88             fetch next from selecttab1_cursor into table_name;
89             while notfound<>1 do
90                 call alter_table_auto_increment(table_name);
91                 
92                 #再次填充数据到变量
93                 fetch next from selecttab1_cursor into table_name;
94             end while;
95         #关闭游标
96         close selecttab1_cursor;
97     end

 

posted @ 2017-02-13 11:08  silentmuh  阅读(1398)  评论(0编辑  收藏  举报
Live2D