乙個儲存過程 游標迴圈結果集

2021-07-30 16:38:08 字數 1579 閱讀 1256

在查詢表名使用變數時只能使用concat 拼接 哭哭哭。。。。

drop procedure

ifexists

proc_tmp;

create

procedure

proc_tmp

()begin

declare

done

intdefault 0;

declare tablename varchar(255);

declare idcur cursor for

select table_name from information_schema.tables where table_schema='record'

and table_name like 'user_%';

declare continue handler for sqlstate '02000'

set done = 1;

open idcur;

repeat

fetch idcur into tablename;

ifnot done then

set @a = tablename;

set @sql_texta =concat('select max(unix_timestamp(time)) into @maxtime from ',@a);

prepare stmta from @sql_texta;

execute stmta;

deallocate prepare stmta;

set @sql_textb =concat('select * from ',@a,' where unix_timestamp(time) <= ',@maxtime,' and unix_timestamp(time) >= ',@maxtime,'- 5');

prepare stmtb from @sql_textb;

execute stmtb;

deallocate prepare stmtb;

endif; until done end

repeat;

close idcur;

end;

call proc_tmp();

在使用mysql.exe 命令列執行時 直接 傳遞編寫好的過程sql檔案 會出錯

cmd指令碼如下

rem @echo off

mysql.exe -h 10.80.1.74 -u root -p < query.sql > result.txt

echo "!!!

!!!!!!

!!!complete!!!

!!!!!!

!!!!!!!"

pause

使用 query.sql檔案 執行時 需要做如此修改

delimiter //

create

procedure

proc_tmp

()....

end //

執行Oracle儲存過程返回游標結果集

create or replace package returncursor is type cur cj is ref cursor procedure find emp out cur cj end returncursor create or replace package body retu...

游標 指向乙個結果集的指標

游標 指向乙個結果集的指標 便於理解的來說 指標就可以當做是這個結果集,迴圈這個指標,就可以拿到該結果集裡的資料 分為 隱式游標 和 顯式游標 隱式游標 select into 需要注意 1 查詢的結果只能是1行,不能是0行或者多行 2 不需要宣告,直接可以使用 顯示游標 聲明顯式游標的語法結構 d...

利用mysql游標迴圈結果集

很多時候自己新增測試資料會用到mysql程式設計,其中儲存過程就非常的重要,所以在這裡寫乙個返回使用者id號用逗號拼接的例子 現在發現mysql完全可以程式設計,一門徹底的程式語言,資料型別還是強型別的,我喜歡 use test delimiter create procedure pro8 out...