记录一个存储过程

101 阅读1分钟

delimiter // # 定义//为一句sql的结束标志,取消;的所代表的意义 drop procedure if exists test; # 如果存在名字为test的procedure则删除 create procedure test() # 创建(创建函数使用的关键字为function 函数名()) begin declare id1 int; declare id2 int; declare flag int default 0; # 这是重点,定义一个游标来记录sql查询的结果(此处的知识点还有SQL的模糊查询,见补充) declare ids cursor for SELECT industry_id,parent_id FROM tb_industry_info # 为下面while循环建立一个退出标志,当游标遍历完后将flag的值设置为1 declare continue handler for not found set flag=1; open ids; # 打开游标 # 将游标中的值赋给定义好的变量,实现for循环的要点 fetch ids into id1,id2; while flag <> 1 do # sql提供了字符串的切分,有left、right、substring、substring_index # 在T-SQL中,局部变量必须以@作为前缀,声明方式set,select还有点差别 # 根据id的唯一性,利用当前查询到的记录中的字段值来实现更新 update tb_enterprise_info_copy set industry_id=id2 where industry_id=id1; # 游标往后移(此处的游标是不是让你想起了C里面的指针) fetch ids into id1,id2; end while; #select * from tb_industry_info; close ids; # 关闭游标 end; delimiter ; # 重新定义;为一句sql的结束标志,取消//的所代表的意义