我知道這是一個基本問題,但我似乎無法拿出正確的搜索詞組合來獲得我需要的答案。如果參數是-1,選擇所有記錄
我正在研究存儲過程,其中有一個名爲@AccountTypeId
的參數。參數可以是1
,2
,3
或-1
。如果@AccountTypeId
是-1
它應該返回全部帳戶類型(1
,2
和3
)。我可以寫一個SP很輕鬆地將過濾器的基礎上@AccountTypeId
:
CREATE PROCEDURE Accounts_SP (@AccountTypeId INT)
AS
SELECT *
FROM Accounts
WHERE AccountTypeId = @AccountTypeId
問題是具有SP返回的所有AccountType
■當@AccountTypeId
是-1
。
我這樣的WHERE
子句中使用CASE
聲明嘗試:
CREATE PROCEDURE Accounts_SP (@AccountTypeId INT)
AS
SELECT *
FROM Accounts
WHERE AccountTypeId IN (CASE @AccountTypeId WHEN -1
THEN (SELECT AccountTypeId FROM AccountTypes)
ELSE @AccountTypeId
END)
但它給我下面的錯誤:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
我敢肯定有一個簡單的方法得到所有的記錄,我只是過分複雜的事情。有人可以請指點我正確的方向嗎?
我知道這是簡單的事情。在時間限制結束後我會接受答案。謝謝!! –