0

我只是设置了一个 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 语句?我不想返回空行......每次该行是第一个查询的“子级别”时,我都会在正确的行之前得到空结果。

我正在考虑创建一个表变量。有没有更好的方法?

4

1 回答 1

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),就好像您已经计算了列一样,那么这会减慢您的过程。

于 2012-06-12T22:21:53.803 回答