mysql變量賦值要注意的 mysqlsql 今天由於項目需要,需要寫個存儲過程,這個可是一年才難得寫一次。於是沒辦法,重新撿起來。開始寫 寫到後來。。
Sql代碼
CREATE FUNCTION `getChildLstnotSun`(department_id int)
RETURNS varchar(1000)
BEGIN
DECLARE sTemp VARCHAR(1000);
DECLARE sTempChd int;
declare fetchSeqOk boolean;
declare cur1 CURSOR FOR SELECT departmentId FROM sys_department_info where fatherDepartmentId=department_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET fetchSeqOk=true;
SET fetchSeqOk=false;
OPEN cur1;
fetchSeqLoop:Loop
FETCH cur1 INTO sTempChd;
if fetchSeqOk then
leave fetchSeqLoop;
else
SET sTemp := concat(sTemp,',',sTempChd);
end if;
end loop;
CLOSE cur1;
RETURN sTemp;
END
需要有個類似於for循環字符串拼接的代碼
Sql代碼
OPEN cur1;
fetchSeqLoop:Loop
FETCH cur1 INTO sTempChd;
if fetchSeqOk then
leave fetchSeqLoop;
else
SET sTemp := concat(sTemp,',',sTempChd);
end if;
end loop;
CLOSE cur1;
結果調試了半天,這個sTemp變量就是不出來. 最後開始奇思妙想。 結果在 SET fetchSeqOk=false;後面加上了 set sTemp=''; 這時候才出來了。尼瑪,原來這個是需要初始化一下的 順便記一下,mysql的group_concat函數最好只用在字符串字段上