我只是设置一个SQL存储过程以按特定顺序返回特定行(从更多到不太重要):
-- 1st Level -> Query a more detailed object...
SELECT ... WHERE something
-- if a result is find, the SP is returning the correct row.
-- if rowcount = 0 then, the SP is returning an empty row
-- and continues with the next query (less specific than the 1st one).
IF @@ROWCOUNT = 0
BEGIN
SELECT .... WHERE something else...
-- Again, if a result exists, the SP returns the correct row, but also
-- the result of the 1st query is returned without rows...
IF @@ROWCOUNT = 0
BEGIN
SELECT .... WHERE something else again...
END
END
是否有任何选项只返回返回非空行的SELECT语句?我不想返回空行...并且每次该行是第一个查询的“子级别”时,我在正确的行之前得到空结果。
我正在考虑创建一个表变量。有没有更好的方法?
答案 0 :(得分:0)
由于您正在寻找缺少行的原因,为什么不在您的过程中使用if / else语句。类似的东西:
If exists(/*firstQuery*/ SELECT ... WHERE something) begin
SELECT ... WHERE something
end else if exists (/*secondQuery*/ SELECT .... WHERE something else... ) begin
SELECT .... WHERE something else...
end else /*Third query*/ begin
SELECT .... WHERE something else again...
end
请记住,当检查存在某些内容时,您应该只选择一列(例如id),就像计算了列一样,这样会减慢您的过程。