在 MySQL Workbench 中创建函数时出错

Error when creating function in MySQL Workbench

我正在尝试在 MySQL Workbench 中定义一个函数。有一个错误显示“@node_id 在此位置无效,需要一个标识符”并且在变量名@node_id 下显示了一条红线。谁能帮我检查一下我的代码问题在哪里?非常感谢!

CREATE FUNCTION dbo.CountLayer  
(  
    @node_id int  
)  
RETURNS int  
AS  
begin  
    declare @result int  
    set @result = 0  
    declare @lft int  
    declare @rgt int  
    if exists(select Node_id from Tree where Node_id = @node_id)  
    begin  
        select @lft = Lft, @rgt = Rgt from Tree where node_id = @node_id  
        select @result = count(*) from Tree where Lft <= @lft and Rgt >= @rgt  
    end  
    return @result  
end  
GO;

正如评论所说,这不是MySql。你不使用 @ 而你必须有 ; 请查看此 link 以及 if-else 语句 here

还要为你的函数检查这个结构

DELIMITER $$
CREATE FUNCTION pl2.test2
(  
    node_id int  
)  
RETURNS int
begin  
    declare result int ; 
     declare  lft int ; 
    declare rgt int;  
    set result = 0 ; 
   your if statment
   
    return result ;

END $$
DELIMITER ;