我有一个带有case子句的简单变量分配,例如:
DECLARE @CurrentType INT
SELECT
@CurrentType = (CASE
WHEN [CL].[ProjectKey] IS NOT NULL
THEN 0
ELSE 1
END)
FROM [Project] AS [P]
LEFT JOIN [CallLog] AS [CL] ON [P].[ProjectKey] = [CL].[ProjectKey]
SELECT
@CurrentType
但是它总是返回NULL而不是1或0
我也尝试使用IIF
有条件的条件,例如:
DECLARE @CurrentType INT = (SELECT
IIF([CL].[ProjectKey] IS NOT NULL , 1 , 0)
FROM [Project] AS [P]
LEFT JOIN [CallLog] AS [CL] ON [P].[ProjectKey] = [CL].[ProjectKey] )
SELECT
@CurrentType
但是,同样的历史,我怎样才能得到欲望的结果?是1还是0?问候
答案 0 :(得分:0)
此表达式:
SELECT @CurrentType = (CASE WHEN [CL].[ProjectKey] IS NOT NULL THEN 0 ELSE 1 END)
FROM [Project] [P] LEFT JOIN
[CallLog] [CL]
ON [P].[ProjectKey] = [CL].[ProjectKey];
如果查询未返回任何行,则只能将@CurrentType
设置为NULL
。假设您使用的是LEFT JOIN
,则意味着Project
为空。
如果将其更改为聚合查询,还将获得一行,因此这可能会满足您的要求:
SELECT @CurrentType = (CASE WHEN MAX([CL].[ProjectKey]) IS NOT NULL THEN 0 ELSE 1 END)
FROM [Project] [P] LEFT JOIN
[CallLog] [CL]
ON [P].[ProjectKey] = [CL].[ProjectKey];