我有一個參數,我需要在where
子句中包含或排除一個條件。 如果CustomerType
參數爲空,那麼我需要包含條件CustomerType is null
,否則我需要排除CustomerType is null
條件。基於參數的動態條件
SQL查詢是:
IF(@CustomerType = Blank)
BEGIN
Select * From Customer
Where
(CustomerType IN(@Type) Or CustomerType is null)
END
Else
Begin
Select * From Customer
Where
CustomerType IN(@Type)
End
有沒有更好的方式來包含或排除不使用IF ELSE
條件像上述這種狀況呢?
'(@type)'不會像你可能希望的那樣工作。 –