如果我的条件得到满足,我想选择特定的元素。请看下面的代码。如何在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 :(得分: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