无法将值写入变量

问题描述 投票:0回答:2

我有一个函数,如果表中有一行包含用户输入的值,则需要返回true。

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

'@ 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语法的其他变体,但没有帮助。

sql sql-server tsql ssms
2个回答
2
投票

将值分配给变量时,需要使用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;

2
投票

[在给SET值时将其放在@ret之前

© www.soinside.com 2019 - 2024. All rights reserved.