儲存過程封裝了一系列的資料庫操作,這樣既方便了程式中呼叫,也加快了資料庫訪問速度。同時一些簡單的邏輯修改也不用直接修改程式,直接修改儲存過程即可。
create definer=`admin`@`%` procedure `addrankex`(in _gameid varchar(32), in _deviceid varchar(64), in _ip varchar(32), in _level int, in _score int, in _cleartime int)
begin
declare _errcode int default 0;
#發生異常時_errcode置1
declare continue handler for sqlexception set _errcode = 1;#非宣告語句必須放在所有宣告語句的後面,否者會報錯。
start transaction;
#初始化引數
set @deviceid = _deviceid;
set @ip = _ip;
set @level = _level;
set @score = _score;
set @cleartime = _cleartime;
set @sameusercount = 0;
set @currenttime = now();
set @best = 0;
set @current = 0;
set @strsql = concat('select count(*) into @sameusercount from ', _gameid, '_rank where deviceid=@deviceid and level=@level');
prepare stmt from @strsql;
execute stmt;
deallocate prepare stmt;
if @sameusercount > 0 then
#更新set @strsql = concat('update ', _gameid, '_rank set score=@score,cleartime=@cleartime,ip=@ip,time=@currenttime where deviceid=@deviceid and level=@level and score(select score from ', _gameid, '_rank where deviceid=@deviceid and level=@level)');
prepare stmt from @strsql;
execute stmt;
deallocate prepare stmt;
set @strsql = concat('select (count(*) + 1) into @current from ', _gameid, '_rank where deviceid!=@deviceid and level=@level and score>@score');
prepare stmt from @strsql;
execute stmt;
deallocate prepare stmt;
select @current as current,@best as best;
else
rollback;#回滾
select _errcode as errcode;
end if;
end
mysql 儲存過程 範例
範例 drop procedure if exists proc delimiter 設定mysql 結束符 否則有可能報莫名其妙的 near at line 錯誤 create oracle or replace procedure proc in iid int,in ipid int out ...
mysql簡單儲存過程範例
範例 begin 定義引數 declare carid int 10 declare v count int declare done int default 1 乙個游標 cursor 可以被看作指向結果集 a set of rows 中一行的指標 pointer declare logistic...
簡單建立儲存過程範例
alter procedure dbo usp syn kmbs batchid int 輸入引數 如果宣告output為輸出引數 asbegin declare username varchar 宣告變數 declare cellphone varchar declare storecode va...