如果在功能中使用

时间:2016-04-11 10:10:09

标签: sql-server tsql

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

{{1}}

1 个答案:

答案 0 :(得分:2)

不需要案例和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
)