mysql 儲存過程 游標的使用

2021-08-05 22:11:08 字數 1776 閱讀 2004

#儲存過程

create procedure changefrozen()

begin

#定義變數

declare i int default 0 ;

declare d int default 0 ;

declare y_id int;

declare y_uid int;

declare y_task_id int;

declare y_frozentime int;

declare y_frozenmoney int;

declare y_unfreezetime int;

declare y_status int;

#定義變數

#使用游標

declare cur_exp cursor for

select id,uid,task_id,frozentime,frozenmoney,unfreezetime,`status` from keke_witkey_frozencapital where `status`= 0 and unfreezetime<=unix_timestamp();

declare continue handler for not found set i=1;

open cur_exp;

fetch cur_exp into y_id,y_uid,y_task_id,y_frozentime,y_frozenmoney,y_unfreezetime,y_status;

while i<> 1 do

if y_id is not null then

update keke_witkey_frozencapital set `status`=1,updatetime=unix_timestamp() where id=y_id and `status`=0;

set @res= (select `status` from keke_witkey_frozencapital where id=y_id limit 1); #變數賦值

if @res>0 then #判斷當前的資料狀態是否發生變化

update keke_witkey_space set balance=balance+y_frozenmoney where uid=y_uid;

set @u_username=(select username from keke_witkey_space where uid=y_uid limit 1); #變數賦值

set @u_balance=(select balance from keke_witkey_space where uid=y_uid limit 1); #變數賦值

#資料插入

insert into keke_witkey_finance(fina_type,fina_action,uid,username,obj_type,obj_id,fina_cash,user_balance,fina_time,fina_mem) values('in','mysql',y_uid,@u_username,'frozen',y_task_id,y_frozenmoney,@u_balance,unix_timestamp(),'凍結資金自動解凍');

end if;

end if;

fetch cur_exp into y_id,y_uid,y_task_id,y_frozentime,y_frozenmoney,y_unfreezetime,y_status;

end while;

close cur_exp;

#游標結束

end;

mysql儲存過程以及游標的使用

create procedure sp1 begin declare s int default 0 declare f total int declare f playerid varchar 30 declare cur cursor for select count as total play...

sql service 儲存過程,游標的使用

1 建表 drop table dbo.users gocreate table dbo.users id int not null name varchar 32 null go alter table dbo.users add primary key id go2 新增資料 刪除儲存過程 if...

儲存過程 事務 游標的使用

語法結構create procedure pro name 引數1 型別 引數n 型別 output 返回引數 as begin 過程 endgo 傳參,接收返回值方式create procedure colo2 iret int output 返回引數 asbegin declare var va...