-1

我对解决这个问题的最佳方法一无所知。当前查询很好。问题是如果没有返回行,我希望 if 语句为真。我也不想写两次查询,所以我如何以 if 语句为真并运行的方式编写它?

IF (select AnInt from ATable where Cond) = expectedInt
begin ... end
4

3 回答 3

3

如果 aNULL AnInt不可能,那么您可以使不匹配时的空子选择结果等于expectedInt

IF isnull((select AnInt from ATable where Cond), expectedInt) = expectedInt
于 2013-01-07T16:19:12.987 回答
2

像这样?

DECLARE @Test INT

SELECT @Test = AnInt
FROM ATable
WHERE Cond

IF @Test IS NULL OR @Test = expectedInt
BEGIN
...
END
于 2013-01-07T16:16:43.570 回答
2
IF Coalesce((select TOP 1 AnInt from ATable where Cond),expectedInt) = expectedInt
begin
end
于 2013-01-07T16:18:39.563 回答