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 ;