reporting-services

Multiple Datasets from Stored Procedure in SSRS


I have a stored procedure that returns multiple resultsets just as below

CREATE StoredProcedure sp_MultipleDataSets
AS
BEGIN
    SELECT EMPID, ENAME, JOB, SAL, DEPTID FROM EMP -- first result set
    SELECT DEPTID, DNAME, LOC FROM DEPT --second result set
END

In BIDS, while creating a new report I configured the stored procedure for dataset. It creates the dataset ONLY with the columns returned from the first result set. It does not identify the second result set.

How can I create datasets for both the result sets from a stored procedure like above


Solution

  • Unfortunately, as the documentation explains here:

    If multiple result sets are retrieved through a single query, only the first result set is processed, and all other result sets are ignored.

    (Found via this question.)

    Therefore, I suggest using one of two possibilities:

    (1) Split the procedure into two separate procedures - one which returns data from EMP, and one from DEPT - and access the new procedures as two separate datasets.

    (2) Union the two separate queries (with an additional column to indicate which query produced each row) and filter or conditionally format your report appropriately. The unioned query might look something like this:

    SELECT EMPID ID, ENAME NAME, JOB JOB_LOC, SAL, DEPTID, 'EMP' SOURCE 
    FROM EMP
    UNION ALL
    SELECT DEPTID ID, DNAME NAME, LOC JOB_LOC, NULL SAL, DEPTID DEPTID, 'DEPT' SOURCE
    FROM DEPT