儲存過程中使用多游標例項 mysql

2021-09-22 17:57:28 字數 1802 閱讀 3941

drop procedure if exists get_qrcode_daily_statistic;

delimiter ;;

create procedure get_qrcode_daily_statistic()

begin

#宣告下面要用到的變數

declare allet_union_out int;

declare ribbon_union_out int;

declare union_out_amount decimal;

declare allet_union_stay int;

declare ribbon_union_stay int;

declare union_stay_amount decimal;

declare allet_company_out int;

declare ribbon_company_out int;

declare company_out_amount decimal;

#建立多個游標

declare unionout cursor for select sum(allet_card) as allet_union_out,sum(ribbon_card) as ribbon_union_out,sum(allet_card_amount) + sum(ribbon_card_amount) as union_out_amount from lh_bill_info where state = 2 and hand_out = 1 and `status` = 1;

declare unionstay cursor for select sum(allet_card) as allet_union_stay,sum(ribbon_card) as ribbon_union_stay,sum(allet_card_amount) + sum(ribbon_card_amount) as union_stay_amount from lh_bill_info where state = 2 and hand_out = 0 and `status` = 1;

#開啟游標

open unionout;

#把游標中資料放入變數中

fetch next from unionout into allet_union_out,ribbon_union_out,union_out_amount;

#關閉當前你游標

close unionout;

#開啟新游標

open unionstay;

#把游標中資料放入變數中

fetch next from unionstay into allet_union_stay,ribbon_union_stay,union_stay_amount;

#關閉當前你游標

close unionstay;

#可使用變數計算變數值

set allet_company_out = allet_union_out + allet_union_stay;

set ribbon_company_out = ribbon_union_out + ribbon_union_stay;

#顯示賦值後的變數值

select allet_union_out,ribbon_union_out,allet_union_stay,ribbon_union_stay,allet_company_out,ribbon_company_out;

end;

;;delimiter ;

call get_qrcode_daily_statistic();

儲存過程中使用游標

create proc cursortest id int 0,name varchar 50 as 建立游標 declare cursor cursor 設定游標欲操作的資料集 set cursor cursor for select id,name from users 開啟游標 open cu...

Mysql儲存過程中使用臨時表和游標

1。臨時表 1 drop procedure2if exists p getmonitorpeople 3create procedure p getmonitorpeople in fgid int,in mins int,in lens int 4 begin 5if fgid 0 mins 0...

儲存過程中的游標使用

利用儲存過程來消除資料庫中冗餘的資料 create procedure sp mytest as declare pro varchar 50 declare mm int declare wu cursor for select distinct product from mytest open ...