sql-server - SSRS - 在一个存储过程中返回多个查询

标签 sql-server stored-procedures reporting-services

我正在尝试创建一个新的 SSRS 报告,该报告将返回并显示 SQL Server 存储过程的值。我将向存储过程传递一个参数@clientID。此参数用在 3 个不同的 BEGIN/END 语句中。每个 BEGIN`END` 语句都会获取参数并进行查询,返回特定数据。

当我创建 SSRS 报告时,我将数据源指向此存储过程,但仅返回第一个 BEGIN/END 语句的结果集。如果我在 SSMS 中运行存储过程,我会得到 3 个不同的结果集,正如预期的那样。

如何将这 3 个 BEGIN/END 结果集放入单个报告中?

示例代码:

CREATE PROCEDURE pClientData (@clientID varchar(30))
AS

    DECLARE @Orders table (
            ...
            ); 

    DECLARE @Results table (
            ...
            );

    DECLARE @Status table (
            ...     
            );

    BEGIN
        SET NOCOUNT ON;

        -- Get all the orders by client
        INSERT INTO @Orders
        SELECT ...


        -- Return the results --
        SELECT *
        FROM @Orders;

    END

    BEGIN
        SET NOCOUNT ON;

        -- Determine the Results

        INSERT INTO @Results
        SELECT ...

        SELECT * 
        FROM @Results;

    END

    BEGIN
        SET NOCOUNT ON;

        SET @Status = (
        SELECT ...
        );

        SELECT @Status as Status;

    END
    GO

来自 SSRS 的查询调用:

EXEC pClientData @clientID

最佳答案

不幸的是,这是不可能的。

根据书Applied Microsoft SQL Server 2008 Reporting Services
来自 第 4.3.5 节 - 使用存储过程:

If a stored procedure returns multiple rowsets (executes multiple SELECT statements), only the first rowset is processed by the report. If you need all results, consider implementing a wrapper stored procedure that merges the multiple rowsets in a temporary table and returns all rows with one SELECT statement.

正如建议的,您必须对存储过程进行某种调整才能完成此任务。创建一个包装器以返回单个集合中的所有结果,或者将现有存储过程拆分为三个。

Note: At the moment, you can get a pdf of the ebook here, but it might get taken down.

关于sql-server - SSRS - 在一个存储过程中返回多个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9727123/

相关文章:

sql-server - Linq to SQL 性能与分组

sql - 将 IIF 函数嵌套在 MAX 函数内

c# - 如何在 C# 中使用存储过程返回结果列表?

java - 从java中的mysql存储过程中获取参数

sql - 在SQL Server 2005 DB上使用Auto Create Statistics =“ON”是否有用?

c# - 如何在异常发生之前检测到导致异常的条件?

c# - sqlDecimal 到十进制 clr 存储过程无法将类型 'System.Data.SqlTypes.SqlDecimal' 的对象转换为类型 'System.IConvertible'

sql - 将非规范化数据移动到报告数据库

c# - 从 Azure 使用 C# 获取 XML 文件作为响应

sql - 嵌套行组上的 SSRS 边框