Select iif sql 多个条件一个结果

Select iif sql multiple conditions in one result

我们有多个变量:SQL Server 2012

中的 Variable1、Variable2 和 Variable3

如果其中一个变量失败,我们将输出失败状态

Declare @Variable1 int
Set @Variable1 = ( select  top 1 count(*)
FROM Table1
where condition1=1 
group by column1,column2
having count(1) >1)

Declare @Variable2 int
Set @Variable2 = ( select  top 1 count(*)
FROM Table1
where condition2=1 
group by column3,column4
having count(1) >1)

Declare @Variable3 int
Set @Variable3 = ( select  top 1 count(*)
FROM Table1
where condition3=1 
group by column5,column6
having count(1) >1)

SELECT IIF ( @Variable1 >=1 and Variable2 >=1 and Variable3 >=1, 'Fail', 'Pass' ) AS ResultTable1;

ResultTable1 始终基于@Variable1 获取结果,而不是基于@Variable2 和@Variable3 结果

有人可以帮忙吗? -谢谢

If one of these variables fail, we out fail status

在这种情况下,您需要在 IIF() 中使用 OR,而不是 AND。按照您现在的方式,所有变量都必须失败才能具有失败状态。

SELECT IIF ( @Variable1 >=1 OR @Variable2 >=1 OR @Variable3 >=1, 'Fail', 'Pass' ) AS ResultTable1