What are alternatives to implement the following query:
有什么替代方法可以实现以下查询:
select *
from table
where isExternal = @type = 2 ? 1 : 0
2 个解决方案
#1
90
Use case
:
用例:
select *
from table
where isExternal = case @type when 2 then 1 else 0 end
#2
112
In SQL Server 2012, you could use the IIF
function:
在SQL Server 2012中,可以使用IIF函数:
SELECT *
FROM table
WHERE isExternal = IIF(@type = 2, 1, 0)
Also note: in T-SQL, the assignment (and comparison) operator is just =
(and not ==
- that's C#)
还要注意:在T-SQL中,赋值(和比较)操作符仅仅是=(而不是= -那是c#)
#1
90
Use case
:
用例:
select *
from table
where isExternal = case @type when 2 then 1 else 0 end
#2
112
In SQL Server 2012, you could use the IIF
function:
在SQL Server 2012中,可以使用IIF函数:
SELECT *
FROM table
WHERE isExternal = IIF(@type = 2, 1, 0)
Also note: in T-SQL, the assignment (and comparison) operator is just =
(and not ==
- that's C#)
还要注意:在T-SQL中,赋值(和比较)操作符仅仅是=(而不是= -那是c#)