简体   繁体   中英

Error when creating function in MySQL Workbench

I am trying to define a function in MySQL Workbench. 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. 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. you don't use @ and you have to have ; please check out this link also for if-else statement here

also check this structure for your 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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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