SQL Server:函数中包含的最后一个语句必须是return语句

时间:2015-05-17 05:49:29

标签: sql sql-server function

我正在编写以下函数来根据我传递给函数的参数检查表中的行并返回数据类型的位结果。

这是功能:

create function dbo.udf_my_func(@var1 varchar(15), @var2 varchar(30))
returns bit as  
begin 
    if not exists (select 1 from Sample_table(nolock) where var1 = @var1 and var2 = 1)
    begin
        return 0
    end

    if exists (select 1 from Sample_table(nolock) where var1 = @var1 and var2 = @var2)
    begin
        return 1
    end

    if exists (select 1 from Sample_table(nolock) where var1 = @var1 and var2 <> @var2)
    begin
        return 0
    end

end
go

但是我收到了这个错误:

  

函数中包含的最后一个语句必须是return语句

请让我知道如何解决这个问题。提前谢谢!

2 个答案:

答案 0 :(得分:0)

您没有为此案例定义返回值:var1&lt;&gt; @var 1

create function dbo.udf_my_func(@var1 varchar(15), @var2 varchar(30))
returns bit as  
begin 


    if not exists (select 1 from Sample_table(nolock) where var1 = @var1 and var2 = 1)
    begin
        return 0
    end


        if exists (select 1 from Sample_table(nolock) where var1 = @var1 and var2 = @var2)
    begin
        return 1
    end

        if exists (select 1 from Sample_table(nolock) where var1 = @var1 and var2 <> @var2)
    begin
        return 0
    end

    -- else
    return 0 
end
go

您可以优化代码:

create function dbo.udf_my_func(@var1 varchar(15), @var2 varchar(30))
    returns bit as  
    begin 


        if exists (select 1 from Sample_table(nolock) where var1 = @var1 and var2 = @var2)
        begin
            return 1
        end
        -- place other conditions that return 1 here

        -- else
        return 0 
    end
    go

答案 1 :(得分:0)

我建议您使用标准类型的函数编写:

CREATE FUNCTION <Scalar_Function_Name, sysname, FunctionName> 
(
    -- Add the parameters for the function here
    <@Param1, sysname, @p1> <Data_Type_For_Param1, , int>
)
RETURNS <Function_Data_Type, ,int>
AS
BEGIN
    -- Declare the return variable here
    DECLARE <@ResultVar, sysname, @Result> <Function_Data_Type, ,int>

    -- Add the T-SQL statements to compute the return value here
    SELECT <@ResultVar, sysname, @Result> = <@Param1, sysname, @p1>

    -- Return the result of the function
    RETURN <@ResultVar, sysname, @Result>

END
GO

所以你的功能应该是这样的:
(请注意,我颠倒了检查的顺序)

CREATE FUNCTION dbo.udf_my_func(@var1 varchar(15), @var2 varchar(30))
RETURNS bit AS  
BEGIN 
    DECLARE @ResultVar bit = 0
    IF EXISTS(SELECT 1 FROM Sample_table(nolock) WHERE var1 = @var1 AND var2 <> @var2)
    BEGIN
        SELECT @ResultVar = 0
    END
    IF EXISTS(SELECT 1 FROM Sample_table(nolock) WHERE var1 = @var1 AND var2 = @var2)
    BEGIN
        SELECT @ResultVar = 1
    END
    IF NOT EXISTS(SELECT 1 FROM Sample_table(nolock) WHERE var1 = @var1 AND var2 = 1)
    BEGIN
        SELECT @ResultVar = 0
    END
    RETURN @ResultVar
END
GO

但你可以使用更好的方法:

CREATE FUNCTION dbo.udf_my_func(@var1 varchar(15), @var2 varchar(30))
RETURNS bit AS  
BEGIN 
    DECLARE @ResultVar bit = 0
    SELECT @ResultVar = CASE 
                            WHEN var1 = @var1 AND var2 <> @var2 THEN 0
                            WHEN var1 = @var1 AND var2 = @var2 THEN 1
                            WHEN var1 = @var1 AND var2 = 1 THEN 0
                            ELSE @ResultVar
                        END
    RETURN @ResultVar
END
GO