如何使用MySQL儲存過程查詢內(nèi)容再批量循環(huán)分表插入
文章來源地址http://www.zghlxwxcb.cn/article/219.html
1、先看基礎(chǔ)結(jié)構(gòu)
drop procedure if exists fenbiao;#如果存在儲存過程則刪除 delimiter $$ #創(chuàng)建一個儲存過程 create procedure fenbiao() begin select count(*) from 表名; #這里放Sql語句【下面2,就是這中間的sql 語句】 end$$ delimiter; #執(zhí)行存儲過程插入數(shù)據(jù) call fenbiao();
文章來源:http://www.zghlxwxcb.cn/article/219.html
2、SQL語句
declare a int default 1000;//設(shè)置變量 declare flag int default 0;//設(shè)置變量,默認(rèn)0的時(shí)候執(zhí)行 declare org_id int(10);// 設(shè)置表里 declare s_list cursor for SELECT `id` as "org_id" FROM `表名` ORDER BY `id` ASC LIMIT 0,3; //測試從這個表里面拿出3條,并且只能ID,賦值為設(shè)置的變量 declare continue handler for not found set flag=1; // 如果while完成,則設(shè)置為1,1 表示結(jié)束 open s_list; // 打開列表 fetch s_list into org_id ; // 從列表里面獲取 org_id while flag <> 1 do // flag != 1 的時(shí)候繼續(xù)執(zhí)行 set @f_id = org_id; // 把ID 賦值給一個變量 set @title = (SELECT title FROM `表名` WHERE id = @f_id); // 設(shè)置一個插入值是從數(shù)據(jù)庫來的。 // INSRTY 這里放插入語句 VALUES 如 VALUES (@f_id, @title) fetch s_list into f_id; end while; close s_list; //關(guān)閉淚飆
到此這篇關(guān)于使用MySQL儲存過程查詢內(nèi)容再批量循環(huán)分表插入或者將查詢結(jié)果插入到另一張表的文章就介紹到這了,更多相關(guān)內(nèi)容可以在右上角搜索或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!