0

我的存储过程 (SQL Server 2005) 返回一个数据集,其中一个字段取决于查询返回的行数。我可以做一个简化的第一个查询,让我得到@@ROWCOUNT,但在这种情况下,该过程返回两组,这不是我想要的。

我尝试将第一个查询放在 WITH 语句中,但没有找到提取行数并将其放入可以在第二个查询中使用的变量中的语法。另一种方法是从第一个查询中获取@@ROWCOUNT 并告诉该过程只返回第二个查询的结果。

可能有更好的方法可以做到这一点,但我在 SQL 方面的专业知识非常有限......

谢谢你的帮助!

4

1 回答 1

3

这是你要找的吗?如果没有,您能否更详细地描述您的问题(也许,使用代码片段)

    alter procedure ComplicatedStoredProcedure as
    begin
        declare @lastQueryRowCount int

        -- Storing the number of rows returned by the first query into a variable.
        select @lastQueryRowCount =
            -- First resultset (not seen by caller).
            (select count(*) from A where ID > 100)

        -- Second resultset. This will be the actual result returned from the SP.
        select * from B where SomeDependentField > @lastQueryRowCount
    end
于 2012-02-26T17:32:53.757 回答