简体   繁体   English

在 MySQL Workbench 中创建 function 时出错

[英]Error when creating function in MySQL Workbench

I am trying to define a function in MySQL Workbench.我正在尝试在 MySQL 工作台中定义一个 function。 There is an error showed that "@node_id is not valid at this position, expecting an identifier" and there is a red line showed under the variable name @node_id.有一个错误显示“@node_id 在此 position 中无效,需要一个标识符”,并且在变量名 @node_id 下显示了一条红线。 Can anyone help me to check where is the problem of my code?谁能帮我检查一下我的代码问题出在哪里? Many Thanks!非常感谢!

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;

it is as the comments say, this is not MySql.正如评论所说,这不是 MySql。 you don't use @ and you have to have ;你不使用@ ,你必须有; please check out this link also for if-else statement here在此处查看此链接以获取 if-else 语句

also check this structure for your function还为您的 function 检查此结构

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 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM