无法将值写入变量

Can not write the value to a variable

我有一个函数,如果 table 中有一行包含用户输入的值,则该函数需要 return 为真。

我需要将值写入 Ii return 的变量,但出现错误:

Incorrect syntax near '@ret'.

USE BDLab5;
GO
Create Function WasComplaint (@date date, @component varchar)
    Returns BIT
    Begin
        Declare @was int, @ret bit
        Select @was = ComponentCode from Complaints 
            Where ComplaintDate = @date AND 
            ComponentCode = (Select ComponentCode from Components Where ComponentName = @component)

        if (@was = 0)  
            @ret = 0
        else 
            @ret = 1 

        Return @ret
    End;

我尝试了 if else 语法的不同变体,但没有用。

当你给它一个值

时,把单词 SET 放在 @ret 之前

给变量赋值时需要使用SET

USE BDLab5;
GO
Create Function WasComplaint (@date date, @component varchar)
    Returns BIT
    Begin
        Declare @was int, @ret bit
        Select @was = ComponentCode from Complaints 
            Where ComplaintDate = @date AND 
            ComponentCode = (Select ComponentCode from Components Where ComponentName = @component)

        if (@was = 0)  
            set @ret = 0
        else 
            set @ret = 1 

        Return @ret
    End;