问题描述
我有一个类似的查询:
DECLARE @tmpValue SET @tmpValue = 0 -- it will be change SELECT * FROM Animal WHERE AniActive = 1 UNION IF @tmpValue > 0 SELECT * FROM Animal WHERE.Active = 0
当我这样使用时,由于 if 条件,它会出错.由于我们的结构,我必须使用 UNION.
When I use like this it is giving error because of if condition. I have to use UNION because of our structure.
如何在 if 条件下使用它?
How can I use it with if condition?
谢谢,
约翰
推荐答案
移动条件 @tmpValue >0 到 WHERE 子句像这样:
Move the condition @tmpValue > 0 to the WHERE clause like so:
SELECT * FROM Animal WHERE AniActive = 1 UNION SELECT * FROM Animal WHERE @tmpValue > 0 AND Active = 0