I saved a tree structure in the department table of the database and looked up all parent departments when the sub-departments are known. the written function is as follows & quot; CREATEFUNCTIONqueryAllDept (@ currdeptidvarchar (50 )) returnsvarchar (200) BEGINdeclare @ allnamevarchar (200) DEFAULT & #39; & #39; declare @ tempnamevarchar (200) DEFAULT & #39; & #39 ;; declare @ tempparidvar mysqlsql function loop query
I have a tree structure in the department table of the database. when we know the sub-department, we can look up all parent departments. the written functions are as follows:
CREATE FUNCTION queryAllDept(@currdeptid varchar(50))returns varchar(200)BEGIN declare @allname varchar(200) DEFAULT ''; declare @tempname varchar(200) DEFAULT ''; declare @tempparid varchar(200) DEFAULT ''; select @allname=`name`,@tempparid=parid from ym_dept where id=@currdeptid; while(@tempparid <> '0') do select @tempname=`name`,@tempparid=parid from ym_dept where id=@tempparid; set @allname=CONCAT(@tempname,',',@allname); end while; RETURN @allname;END
Error message:
[Err] 1064-You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@ currdeptid varchar (50 ))
Returns varchar (200)
BEGIN
Declare @ allname varchar 'at line 1
Where is the problem?