關於mysql儲存過程

2021-05-25 02:18:29 字數 2358 閱讀 5348

刪除 :drop procedure if exists 名稱; 

建立例項:

create procedure add_caller

(myuid int,

fwuid int,

dt datetime,

out returnvalue int

)begin

declare exresult int;

declare dtbaseiff int;

declare countcall int;

declare callnumid int;

select datediff(dt, checkday) into dtbaseiff  from hot_userinfo where uid=myuid;

if dtbaseiff is null then

set dtbaseiff=1;

end if;

if dtbaseiff >0  then

update hot_userinfo set daychecknum=1,checkday=date_format(dt,'%y%m%d')  where uid=myuid;

end if;

if dtbaseiff<=0 then

update hot_userinfo set daychecknum=daychecknum+1 where uid=myuid;

end if;

update hot_userinfo set callernum=callernum+1 where uid=myuid;

set exresult=row_count();

if exresult is null then

set exresult=0;

end if;

if exresult >0 then

select count(*) into countcall from hot_caller where uid=myuid;

select callid into callnumid  from hot_caller where uid=myuid and calluid=fwuid;

if callnumid is null then

set callnumid=0;

end if;

if countcall >= 20 and callnumid <=0 then

delete from hot_caller where uid=myuid order by calltime asc limit 1;

end if;

if callnumid <=0 then

insert into hot_caller(uid,calluid,calltime,marking) values(myuid,fwuid,dt,0);

end if;

if callnumid>0 then

update hot_caller set calltime=dt where callid=callnumid;

end if;

end if;

set returnvalue = exresult;

endcreate procedure add_votestar

(receuid int,

senduid int,

tpnum int,

dt datetime,

out returnvalue int

)begin

declare exresult int;

select count(*) into exresult from hot_votestar where rid=receuid and sid=senduid and date_format(votetime,'%y-%m-%d')=date_format(dt,'%y-%m-%d');

if exresult is null then

set exresult=0;

end if;

if exresult <=0 then

insert into hot_votestar(rid,sid,score,votetime) values (receuid,senduid,tpnum,dt);

set exresult=row_count();

if exresult is null then

set exresult=0;

end if;

else if exresult >0 then

set exresult=2;

end if;

end if;

set returnvalue = exresult;

end

mysql儲存過程 MySQL儲存過程

在本節中,您將逐步學習如何在mysql中編寫和開發儲存過程。首先,我們向您介紹儲存過程的概念,並討論何時使用它。然後,展示如何使用過程 的基本元素,如建立儲存過程的語句,if else,case,loop,儲存過程的引數。下面每個教程都包含了易於理解的示例和詳細的說明。如果您瀏覽並學習所有教程,您可...

mysql 儲存過程 mysql 儲存過程

建立 為建立儲存過程的結束標誌,使用delimiter 可更改標誌 格式create procedure begin sqlend create procedure myprocedure in param integer begin select from tb role where tb rol...

mysql 儲存過程 MySQL儲存過程

目錄 儲存過程 簡介是一組為了完成特定功能的sql語句集合 比傳統sql速度更快 執行效率更高 儲存過程的優點 執行一次後,會將生成的二進位制 駐留緩衝區,提高執行效率 sql語句加上控制語句的集合,靈活性高 在伺服器端儲存,客戶端呼叫時,降低網路負載 可多次重複被呼叫,可隨時修改,不影響客戶端呼叫...