Sql server 在函数中使用if

Sql server 在函数中使用if,sql-server,tsql,Sql Server,Tsql,我使用此查询,但它不起作用: create FUNCTION getUserCreditPayment ( @resellerCode int, @userCode int, @startDate datetime, @endDate datetime ) RETURNS TABLE AS RETURN( WITH Directories AS ( CASE WHEN (@userCode != 0)

我使用此查询,但它不起作用:

create FUNCTION getUserCreditPayment
(
    @resellerCode int,
    @userCode int,
    @startDate datetime,
    @endDate datetime
)
RETURNS TABLE AS
RETURN(
    WITH Directories AS 
    (
        CASE 
            WHEN (@userCode != 0)  
            THEN 
                (SELECT * 
                 FROM UserCredit 
                 WHERE userCode = @userCode 
                   AND date >= @startDate AND date < @endDate
                UNION ALL 
                SELECT code, date, price* - 1, 
                       userCode, userCodeDes, customerUserName, type 
                FROM UserPayment 
                WHERE userCode = @userCode 
                  AND date >= @startDate AND date < @endDate)
            ELSE
                (SELECT * 
                 FROM UserCredit 
                 WHERE userCode = @userCode 
                   AND date >= @startDate AND date < @endDate
                UNION ALL 
                SELECT code, date, price* -1,
                       userCode, userCodeDes, customerUserName, type 
                FROM UserPayment 
                WHERE date >= @startDate AND date < @endDate)
         END
    )
    SELECT * 
    FROM Directories
)

不需要Case和if语句,两个脚本中唯一的区别是可以使用OR条件处理的用户代码检查

试试这个

create FUNCTION getUserCreditPayment
(
    @resellerCode int,
    @userCode int,
    @startDate datetime,
    @endDate datetime
)
RETURNS TABLE AS
RETURN(
    WITH Directories AS 
    (

         SELECT  * FROM UserCredit where userCode=@userCode and date>=@startDate and date<@endDate
            UNION ALL SELECT code,date,price*-1,userCode,userCodeDes,customerUserName,type from UserPayment 
            where (userCode=@userCode OR @userCode = 0)  and date>=@startDate and date<@endDate


    )
    SELECT  * FROM   Directories
)

如果要检查多个条件并插入,则应选择多表值函数。下面是一个示例

create FUNCTION getUserCreditPayment
(
   @id int
)
RETURNS --this is the table which you will return,Populate it with same schema
@test table 
(
id int,
name varchar(max)
)
as
Begin
if (@userCode != 0)
begin
insert into @test
SELECT  * FROM UserCredit where userCode=@userCode and date>=@startDate and date<@endDate
            UNION ALL SELECT code,date,price*-1,userCode,userCodeDes,customerUserName,type from UserPayment 
            where userCode=@userCode and date>=@startDate and date<@endDate
end
else 
insert into @test
SELECT  * FROM UserCredit where userCode=@userCode and date>=@startDate and date<@endDate
            UNION ALL SELECT code,date,price*-1,userCode,userCodeDes,customerUserName,type from UserPayment 
            where userCode=@userCode and date>=@startDate and date<@endD

Return
end

我这边的Upvote,特别是因为您使用的是没有BEGIN和END的内联TVF语法。这个答案使用了BEGIN和END的旧语法,因此性能非常差。。。很少有情况下,您无法使用新的可内联的ad-hoc语法达到相同的效果,而这种语法的性能要好得多!谢谢你的提示,更新,你能分享一些例子吗?嗨,对不起,我不够清楚。我指的是单语句TVF,它的性能比多语句函数好得多。看看Naveen Kumars的回答。这里有一个返回表作为。。。然后是一个裸查询。否插入否@变量否IF-ELSE。。。