默認情況下,mysql解釋器一遇到分號(;),它就要自動執行。
不會等到用戶把這些語句全部輸入完之后,再執行整段語句。
而自定義函數和存儲過程的SQL語句有好多行,且語句中包含有分號,
為了保證整段語句的整體執行,就要使用delimiter,更改mysql的默認結束符。
再看一個列子:
delimiter $$
use `test`$$
drop function if exists `queryOnePartnerInviteCodeByCode`$$
create function `queryOnePartnerInviteCodeByCode`(_code varchar(4000)) returns varchar(4000) charset utf8
begin
set _code=TRIM(_code);
if _code is null or _code='' then
return null;
end if ;
select GROUP_CONCAT(id) into @__userID from lcs_member where del_flag=0 and invite_code=_code;
if @__userID is null or @__userID='' then
return null;
end if ;
select GROUP_CONCAT(id),GROUP_CONCAT(invite_code) into @__selectTmpID,@_invite_code from lcs_member where del_flag=0 and invite_code!='' and invite_code is not null and belong_to_partner>0 and FIND_IN_SET(belong_to_partner,@__userID)>0;
if (@__selectTmpID!='' and @__selectTmpID is not null ) then
return @_invite_code;
else
return null;
end if;
end$$
delimiter;