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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章