--儲存過程名和引數,引數中in表示傳入引數,out標示傳出引數,inout表示傳入傳出引數
create procedure p_procedurecode(in sumdate varchar(10))
begin
declare v_sql varchar(500); --需要執行的sql語句
declare sym varchar(6);
declare var1 varchar(20);
declare var2 varchar(70);
declare var3 integer;
--定義游標遍歷時,作為判斷是否遍歷完全部記錄的標記
declare no_more_departments integer default 0;
--定義游標名字為c_result
declare c_result cursor for
select barcode,barname,barnum from tmp_table;
--宣告當游標遍歷完全部記錄後將標誌變數置成某個值
declare continue handler for not found
set no_more_departments=1;
set sym=substring(sumdate,1,6); --擷取字串,並將其賦值給乙個遍歷
--連線字串構成完整sql語句,動態sql執行後的結果記錄集,在mysql中無法獲取,因此需要轉變思路將其放置到乙個臨時表中(注意**中的寫法)。一般寫法如下:
-- 'create temporary table 表名(select的查詢語句);
set v_sql= concat('create temporary table tmp_table(select aa as aacode,bb as aaname,count(cc) as ccnum from h',sym,' where substring(dd,1,8)=''',sumdate,''' group by aa,bb)');
set @v_sql=v_sql; --注意很重要,將連成成的字串賦值給乙個變數(可以之前沒有定義,但要以@開頭)
prepare stmt from @v_sql; --預處理需要執行的動態sql,其中stmt是乙個變數
execute stmt; --執行sql語句
deallocate prepare stmt; --釋放掉預處理段
open c_result; --開啟之前定義的游標
fetch c_result into var1, var2, var3; --取出每條記錄並賦值給相關變數,注意順序
--執行查詢語句,並將獲得的值付給乙個變數 @oldaacode(注意如果以@開頭的變數可以不用通過declare語句事先宣告)
select @oldaacode:=vcaacode from t_sum where vcaacode=var1 and dtdate=sumdate;
if @oldaacode=var1 then --判斷
update t_sum set inum=var3 where vcaacode=var1 and dtdate=sumdate;
else
insert into t_sum(vcaacode,vcaaname,inum,dtdate) values(var1,var2,var3,sumdate);
end if;
until no_more_departments end repeat; --迴圈語句結束
close c_result; --關閉游標
drop temporary table tmp_table; --刪除臨時表
end;
drop database if exists wifi_database;
create database wifi_database default character set utf8;
use wifi_database;
create table wifi_table(
rowid varchar(255),
id varchar(255),
name varchar(255),
bssid varchar(255),
level varchar(255),
ssid varchar(255)
)default charset=utf8;
charset utf8;
load data infile 'c:/wifi_data.csv'
into table wifi_table character set utf8
fields terminated by ',' optionally enclosed by '"' escaped by '"'
lines terminated by '\r\n';
-- -- 單條測試
-- select * from (
-- -- union all:不刪除重複;union:刪除重複
-- select 'name','bssid','level','ssid' union all
-- -- 外面套一層select * from,不然order by會被union弄失效了
-- select * from (select name,bssid,level,ssid from wifi_table where name = '(1,0)' order by ssid) tbl_order
-- ) tbl_add_title
-- into outfile 'c:/wifi/1_0.txt'
-- fields terminated by ',' optionally enclosed by '' escaped by ''
-- lines terminated by '\r\n';
-- 定義結束符為 $$
delimiter $$
-- 刪除 已有的 儲存過程
drop procedure if exists wifi_table_to_file;
-- 建立新的儲存過程
create procedure wifi_table_to_file()
begin
-- 變數宣告
declare v_sql varchar(500);-- 需要執行的sql語句
declare i int;-- 1,2
declare j int;-- 0-79
-- 迴圈體
set i = 1;
while i <= 2 do
set j = 0;
while j <= 79 do
set v_sql= concat('select * from (select \'name\',\'bssid\',\'level\',\'ssid\' union all select * from (select name,bssid,level,ssid from wifi_table where name = \'(',i,',',j,')\' order by ssid) tbl_order) tbl_add_title into outfile \'c:/wifi/',i,'_',j,'.txt\' fields terminated by \',\' optionally enclosed by \'\' escaped by \'\' lines terminated by \'\\r\\n\';');
-- 預處理需要執行的動態sql,其中stmt是乙個變數
set @v_sql_for_stmt=v_sql;
prepare stmt from @v_sql_for_stmt;
-- 執行sql語句
execute stmt;
-- 釋放掉預處理段
deallocate prepare stmt;
set j = j +1;
end while;
set i = i +1;
end while;
end $$
-- 先把結束符 回覆為;
delimiter ;
call wifi_table_to_file();
mysql動態執行儲存過程語句
mssql中動態執行sql語句可以使用exec 函式。mssql中也有類似的函式execute 不過不同的是mysql中動態執行儲存過程語句與mssql還是有區別的 下面寫乙個給大家做參考啊 create procedure sp find pfind varchar 500 begin decla...
寫MySQL儲存過程實現動態執行SQL
儲存過程名和引數,引數中in表示傳入引數,out標示傳出引數,inout表示傳入傳出引數 create procedure p procedurecode in sumdate varchar 10 begin declare v sql varchar 500 需要執行的sql語句 declare...
mysql 儲存過程 動態引數 查詢執行結果
mysql function procedure 學習使用小結 1 動態sql,即動態引數 在儲存過程中,想要直接用表名變數做引數,動態執行sql,不能直接寫 1 2 3 4 5 6 7 createprocedure tablenamechar 20 begin select fromtablen...