我需要将此c#行转换为SQL查询:
from t in table where type == 0 ? t.colA=1 : t.ColB=1
我尝试过
select * from table t where
BEGIN IF @type=0 THEN t.colA=1 END
BEGIN IF @type=1 THEN t.colB=1 END
但我明白了
Incorrect syntax near the keyword 'BEGIN'.
和
Incorrect syntax near the keyword 'THEN'.
我在做什么错?甚至有可能作为SQL命令来执行此操作?
答案 0 :(得分:4)
只需使用布尔表达式:
select *
from table t
where (@type = 0 and t.colA = 1) or
(@type = 1 and t.colB = 1)
如果您想要@type
不为0或1时的所有内容,请添加:
select *
from table t
where (@type = 0 and t.colA = 1) or
(@type = 1 and t.colB = 1) or
(@type not in (0, 1)) -- adjust if @type could be NULL
答案 1 :(得分:3)
您可以使用CASE
:
select * from table t
where (CASE @type WHEN 0 THEN colA
WHEN 1 THEN colB
END) = 1
答案 2 :(得分:0)