2015-09-28 192 views
0

如果滿足條件,我想選擇特定的元素。請看下面我的代碼。我如何選擇CASE子句中的數據?我的辦法不起作用如果條件滿足,選擇參數

CREATE PROCEDURE [dbo].[GetUsers] 
@firstName varchar(100) = '', 
@isCounterSelected int, 
@countries dbo.tvp_stringArray READONLY 
AS 

BEGIN 
    SET NOCOUNT ON; 

SELECT [PK_UserID] AS Id 
     ,[UserNickName] 
     ,[description] 
     ,CASE 
     WHEN @isCounterSelected = 1 THEN (NULL AS MCount, NULL AS GCount, NULL AS CCount) 
     ELSE '' 
     END 
     FROM [dbo].[User_Existing]  
     WHERE (@firstName NOT LIKE '' AND firstName LIKE @firstName + '%') 
     AND Country IN (SELECT inputValue FROM @countries) 
+0

什麼是你的問題? –

+0

對不起。我剛剛編輯過。 – Matt

+0

你現在可以檢查你的CASE語句嗎? – ProblemSolver

回答

1

我認爲這將是有語法錯誤固定:

CREATE PROCEDURE [dbo].[GetUsers] 
@firstName varchar(100) = '', 
@isCounterSelected int, 
@countries dbo.tvp_stringArray READONLY 
AS 

BEGIN 
    SET NOCOUNT ON; 

SELECT [PK_UserID] AS Id 
     ,[UserNickName] 
     ,[description] 
     ,CASE 
     WHEN @isCounterSelected = 1 THEN NULL 
     ELSE '' 
     END AS MCount 
     ,CASE 
     WHEN @isCounterSelected = 1 THEN NULL 
     ELSE '' 
     END AS GCount 
     ,CASE 
     WHEN @isCounterSelected = 1 THEN NULL AS CCount 
     ELSE '' 
     END AS CCount 
     FROM [dbo].[User_Existing]  
     WHERE (@firstName NOT LIKE '' AND firstName LIKE @firstName + '%') 
     AND Country IN (SELECT inputValue FROM @countries) 
END 
+0

謝謝,但我不想在條件未滿足時添加附加列 – Matt

+0

但這就是Sql的工作原理。如果你選擇3行,那麼我們也會有3個else子句。 – ProblemSolver

+0

我的意思是...當@isCounterSelected = 0我想選擇只是PK_UserID,UserNickName和說明 – Matt