bitscn.com
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函数最好只用在字符串字段上
bitscn.com