我有一个类似的查询:
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
这篇关于Sql Server 联合与 If 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!