动态Where子句T-SQL varibable运算符

时间:2017-03-29 05:28:40

标签: sql-server tsql dynamic sql-server-2012 where-clause

我试图根据存储过程的输入变量在sql中设置我的where条件。

Where RF.InternalCompany = @Company  
And RF.Installdate between @Datefrom  and @DateTo
AND CASE WHEN @ContractType = 'Single Asset Contract' THEN numA.NumOfAssets = 1 
    WHEN @ContractType = 'Multi-Asset Contract' THEN numA.NumOfAssets > 1
    END

如何设置?

2 个答案:

答案 0 :(得分:2)

如下所示进行更改..

Where RF.InternalCompany = @Company  
And RF.Installdate between @Datefrom  and @DateTo
AND ((@ContractType = 'Single Asset Contract' AND     numA.NumOfAssets = 1 ) Or 
(@ContractType = 'Multi-Asset Contract' AND numA.NumOfAssets > 1))

答案 1 :(得分:1)

AND 
(
    ( @ContractType = 'Single Asset Contract' AND numA.NumOfAssets = 1 )
    OR 
    ( @ContractType = 'Multi-Asset Contract'  AND numA.NumOfAssets > 1 )
)