SQLServer儲存過程

2021-07-30 05:07:23 字數 4193 閱讀 9771

(@action varchar(100)

,@game_id int=2

,@lot_type varchar(12)

,@weixin_id varchar(32)

,@seq varchar(32)=''

,@source varchar(50)='')as

begin

set nocount on;

declare @success varchar(32);

declare @code nvarchar(32);

declare @msg nvarchar(128);

declare @rnd int;

declare @lot_id int;

declare @lot_code nvarchar(32);

--預設錯誤資訊

set @code='9997'

set @msg='code_parameter_invalid'

set @success='false'

if (@action='api.h5.game.draw')

goto action_h5_game_draw;

else

goto action_error

action_h5_game_draw:

print 'action_reg:' + @action

--檢查player_code是否已經註冊

if not exists (select 1 from h5_game_player where player_code=@weixin_id)

--以前沒有抽過獎的直接插入資料,play_id是對應籤的型別1到8

begin

insert into h5_game_player (game_id,player_code,play_num,played_num,created,seq,status,player_id)values(@game_id,@weixin_id,2,1,getdate(),@seq,0,@lot_type);

--防止序列重複

if not exists (select 1 from h5_game_play_log where seq=@seq)

insert into h5_game_play_log (game_id,player_code,seq,created,from_code) values(@game_id,@weixin_id,@seq,getdate(),@source);

--去**

goto action_draw;

endelse

begin

if (select count(0) from h5_game_player where player_code=@weixin_id and points=1)>0

begin

set @code = '8889';

set @msg = '您已經中過獎了,不能繼續參與**';

goto action_msg_end;

endif (select count(0) from h5_game_play_log where player_code=@weixin_id and game_id=@game_id)>=2

begin

set @code = '9000';

set @msg = '您已經抽取兩次了';

goto action_msg_end;

endif exists (select 1 from h5_game_play_log where player_code=@weixin_id and game_id=@game_id and from_code=@source)

begin

set @code = '9001';

if (@source='pulicno')

else

goto action_msg_end;

end--插入**記錄,同時更新**總數和能否**的狀態

insert into h5_game_play_log (game_id,player_code,seq,created,from_code) values(@game_id,@weixin_id,@seq,getdate(),@source);

update h5_game_player set status=1,played_num=2 where player_code = @weixin_id;

--去**

goto action_draw;

end--返回資訊

goto action_msg_end;

--**

action_draw:

begin

select @rnd=cast( floor(rand()*9999) as int);

if(@rnd<500)

begin

--select @lot_id = (select top 1 id from h5_game_award_pool where award_id=@lot_type and action_id=0 and game_id=@game_id and start_date < getdate());

select @lot_id = (select top 1 p.id from h5_game_award_pool p left join h5_game_award_config f on p.award_id = f.id where f.award_code=@lot_type and p.action_id=0 and p.game_id=@game_id and p.start_date < getdate());

if(@lot_id>0)

begin

update h5_game_award_pool set action_id=1,player_code=@weixin_id,updated=getdate() where id = @lot_id;

--points作為是否中獎的標識1:中獎,0:未中獎

update h5_game_player set points=1 where player_code = @weixin_id;

select 'true' as success,0 as code,'恭喜您中獎了' as msg,@weixin_id as weixin_id,exchange_code as lot_code,

shop_name as brand_name,award_title,comments as award_name,award_price,award_uri from h5_game_award_pool p

left join h5_game_award_config f on p.award_id = f.id where p.id=@lot_id

return

end

endelse

begin

select 'true' as success,0 as code,'您沒有中獎了' as msg,'' as lot_code,'' as weixin_id

return

endend;

--錯誤,無此方法

action_error:

--print @action

print 'error'

select @success as success,@code as code,@msg as msg,'' as lot_code,'' as weixin_id

return

--直接結束

action_ok:

print 'ok:' + @action + ' ' + @weixin_id

return

--顯示返回資訊,並結束

action_msg_end:

set @success='true'

print 'ok:' + @action + ' ' + @weixin_id

select @success as success,@code as code,@msg as msg,'' as lot_code,'' as weixin_id

return

set nocount off;

end

sqlserver的儲存過程呼叫跟mysql是有區別的:

mysql是:

call prc_add_si_member_point(1,'%s','%s','%s'); 帶括號

sql server儲存過程

建立表的語句 create table student sno int primary key,sname nvarchar 30 sgentle nvarchar 2 sage int,sbirth smalldatetime,sdept nvarchar 30 drop table studen...

SQLSERVER儲存過程

sqlserver儲存過程使用說明書 引言首先介紹一下什麼是儲存過程 儲存過程就是將常用的或很複雜的工作,預先用 sql語句寫好並用乙個指定的名稱儲存起來,並且這樣的語句是放在資料庫中的,還可以根據條件執行不同 sql語句,那麼以後要叫資料庫提供與已定義好的儲存過程的功能相同的服務時,只需呼叫 ex...

SQL Server 儲存過程

儲存過程概念 儲存過程優點 儲存過程的介面 儲存過程的解析 編譯過程 儲存過程安全性 檢視儲存過程 加密 解密儲存過程 儲存過程概念 儲存過程 stored procedure 是一組為了完成特定功能的sql語句集,經編譯後儲存在資料庫中。使用者通過指定儲存過程的名字並給出引數 帶參儲存過程 來執行...