作者:龙晓桥的微博_267 | 来源:互联网 | 2023-09-17 21:48
####这篇博文写了什么?#####Mysql数据库中将表的数据迁移到另一个表怎么做?#####Mysql数据库中存储过程脚本结构?#####Mysql数据库中存储过程中的变量定义
####这篇博文写了什么?
#####Mysql数据库中将表的数据迁移到另一个表怎么做?
#####Mysql数据库中存储过程脚本结构?
#####Mysql数据库中存储过程中的变量定义、赋值、改变?
#####Mysql数据库中存储过程中的IF判断?
#####Mysql数据库中存储过程中的LOOP循环?
#####Mysql数据库中存储过程中的游标操作?
#####Mysql数据库中存储过程中的获取自增主键的值?
#####Oracle对标准SQL拓展的语言叫PLSQL,SQLServer对标准SQL拓展的语言好像叫t-SQL,那Mysql对标准SQL拓展的语言叫什么???搜了搜,竟然没找到。
要想在Mysql数据库中对表的数据取出、处理一下再存起来,使用存储过程是最方便的选择,这里使用存储过程仅仅是为了给处理数据一个空间,所以在脚本中用完之后删掉即可。
#####1.存储过程脚本的基础结构
create procedure syscode() --创建一个存储过程
begin --存储过程的开始
--具体操作
end; --存储过程的结束
call syscode(); --调用存储过程
drop procedure if exists syscode --删除存储过程
#####2.变量的定义、赋值、改变
declare i int default 5; --定义初始值为5的int变量
declare text varchar(200); --定义varchar变量
set i = 6; --改变变量的值
set i = i+1; --变量的自加
#####3.IF判断
--判断数
if (i = 7) then
set i = 8;
end if;
--判断bool
declare done int default false;
if done then
set i = 8;
end if;
#####4.LOOP循环和跳出
read_loop: LOOP --read_loop不需要事先定义
--具体操作
--判断跳出
if done then
leave read_loop;
end if;
end LOOP;
#####5.游标的定义、遍历
什么是游标,自行百度吧。
//不知道mysql的存储过程里有没有行类型这种东西,所以就定义多个变量来存一条记录的各个字段。
declare calssIdP int;
declare nameP varchar(32);
declare ageP int;
//定义用于跳出遍历的标识
declare done int default false;
//定义游标
declare cursorName cursor for select classId,name,age from students;
//绑定游标和标识
declare continue handler for not found set dOne= true;
//打开游标
open cursorName;
//循环遍历
read_loop: LOOP
fetch next from cursorName into calssIdP,nameP,ageP;
if done then
leave read_loop;
end if;
//假装数据迁移
insert into 另一个表(age,name) values(ageP, nameP);
end LOOP;
//关闭游标
close cursorName;
#####6.获取刚刚insert的记录的自增主键的值
mysql数据库支持自增主键,我在存储过程中对一个表进行insert一条新的记录,获取刚刚新增的记录的主键。
declare nextid int;
insert into student(name,age) values('li', 12); --插入一条新记录
select max(stu_id) from student into nextid; --获取插入的记录的自增id
#####7.直接贴一个数据迁移的脚本吧,读者可以结合上面看
这是将sys_code_tl表中的数据存入sys_descriptions后,获得自增ID再存入sys_code表。注意这里sys_descriptions表是以id和lang两个字段为主键,所以脚本中可以对同一个id存两条数据,这是具体需求要求的。
create procedure syscode()
begin
declare i int default 5;
declare nextid int;
declare fflag int default 0;
declare codeidP int;
declare langP varchar(32);
declare descriptionP varchar(200);
declare done int default false;
declare syscodeCur cursor for select sc.code_id,sc.lang,sc.description from sys_code_tl sc order by sc.code_id;
declare continue handler for not found set dOne= true;
open syscodeCur;
read_loop: LOOP
fetch next from syscodeCur into codeidP,langP,descriptionP;
if done then
leave read_loop;
end if;
insert into sys_descriptions(language,description_text,ref_table,ref_field)
values(langP, descriptionP,'SYS_CODE','DESCRIPTION');
select max(description_id) from sys_descriptions into nextid;
update sys_code set code_description_id = nextid where code_id = codeidP;
fetch next from syscodeCur into codeidP,langP,descriptionP;
if done then
leave read_loop;
end if;
insert into sys_descriptions(description_id,language,description_text,ref_table,ref_field)
values(nextid, langP, descriptionP,'SYS_CODE','DESCRIPTION');
update sys_code set code_description_id = nextid where code_id = codeidP;
end LOOP;
close syscodeCur;
end;
call syscode();
drop procedure if exists syscode