Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « You should be watchingWhat #sqlfamily Means To Me (Or, A Case of the Warm Fuzzies) »
    comments

    It’s TSQL Tuesday #024! It’s been two years and the monthly blog party is going strong. Thanks to Brad Schulz for hosting this month!

    This month’s topic is “Prox n' Funx”. I am going to show something that frustrates me, and, from what I see on forums, a lot of other people. When building a dataset in Reporting Services that calls a stored procedure, if the stored procedure has multiple result sets, only the first is returned.

    I’m using SQL Server Reporting Services 2008R2 and AdventureWorks2008R2. I built a stored procedure that returns all products in a category, then a count of the number of products in the category, and the count of the number of products in that category in a specific color.

    1. ALTER PROCEDURE [dbo].[ProductCountByCatColor]
    2.     @Category varchar(25),
    3.     @Color varchar(25)
    4. AS
    5. BEGIN
    6.     SET NOCOUNT ON;
    7.  
    8.     SELECT PC.Name AS Category,
    9.         PROD.ProductNumber,
    10.         PROD.Name,
    11.         ISNULL(PROD.Color, 'N/A') AS Color,
    12.         ISNULL(PROD.Size, 'N/A') AS Size,
    13.         ISNULL(PROD.[Weight], 0) AS Weight
    14.     FROM Production.Product PROD
    15.         INNER JOIN Production.ProductSubcategory PS ON PS.ProductSubcategoryID = PROD.ProductSubcategoryID
    16.         INNER JOIN Production.ProductCategory PC ON PC.ProductCategoryID = PS.ProductCategoryID
    17.     WHERE PC.Name IN (@Category)
    18.     ORDER BY Category
    19.  
    20.     SELECT PC.Name AS Category,
    21.         COUNT(PROD.ProductNumber) as ProdCount
    22.     FROM Production.Product PROD
    23.         INNER JOIN Production.ProductSubcategory PS ON PS.ProductSubcategoryID = PROD.ProductSubcategoryID
    24.         INNER JOIN Production.ProductCategory PC ON PC.ProductCategoryID = PS.ProductCategoryID
    25.     WHERE PC.Name IN (@Category)
    26.     GROUP BY PC.Name
    27.     ORDER BY PC.Name
    28.  
    29.     SELECT PC.Name AS Category,
    30.         COUNT(PROD.ProductNumber) as ProdCount
    31.     FROM Production.Product PROD
    32.         INNER JOIN Production.ProductSubcategory PS ON PS.ProductSubcategoryID = PROD.ProductSubcategoryID
    33.         INNER JOIN Production.ProductCategory PC ON PC.ProductCategoryID = PS.ProductCategoryID
    34.     WHERE PC.Name IN (@Category)
    35.         AND ISNULL(PROD.Color, 'N/A') IN (@Color)
    36.     GROUP BY PC.Name
    37.     ORDER BY PC.Name
    38. END

    When I execute the SP in SSMS, I get three result sets.


    Now, I set up my report to use a stored procedure in the dataset.


    I don’t even need to run the query to notice that the dataset only shows fields from the first result set.


    When I run the report, I only see one set of results – the first listed in my stored procedure.


    Why? The short answer is: it’s designed that way. According to BOL, “Multiple results sets from a single query are not supported.” (http://msdn.microsoft.com/en-us/library/dd239379.aspx)

    How can you work around this?

    If each result set is a separate query, you can put each query in a separate stored procedure, or dataset.

    If the result sets need to tie together, you’ll need to work with the T-SQL to make it one result set. That may require temp tables, UNIONs, or another solution.

    That’s my short-and-sweet contribution to #tsql2sday24. Thanks for hosting, Brad!

    About the Author

    User bio imageJes Schultz Borland is a Consultant with Brent Ozar PLF, and a Microsoft SQL Server MVP. She holds an AAS - Programmer/Analyst degree, and has worked with SQL Server since 2007, focusing on Reporting Services and day-to-day administration. She is an active member of PASS, Director of Communication for MADPASS, founder of Tech on Tap, and a LessThanDot.com blogger. She's a frequent presenter at user groups, SQL Saturdays, and other community events. She is also an avid runner and chef.
    Social SitingsTwitterLinkedInLTD RSS Feed
    Instapaper

    3 comments

    Comment from: David Forck (thirster42) [Member]
    It's like that because the data sets aren't designed to have "sub datasets". A data set is just that, a singular set of data returned from the data source. If you need different pieces of data then you need different data sets for each piece.

    Anyway, great article!
    11/08/11 @ 13:45
    Comment from: jonmcrawford [Visitor] Email · http://www.linkedin.com/in/jonmcrawford
    jonmcrawford Separate datasets/unions would be necessary if you had any detail within your 2nd and 3rd queries, but if all you want is counts, you could also use expressions to find the values and drop into a textbox (I'd have to play with filters to really get it working correctly):
    ="This category contains " & CountDistinct(Fields!ProductNumber.Value,"DataSet1") & " product(s)."

    11/08/11 @ 14:28
    Comment from: Jes Schultz Borland (grrlgeek) [Member]
    Jes Schultz Borland (grrlgeek) Jon, you're correct. This is a simplistic example where the stored procedure contains three very similar queries. Expressions could be built from the first query.
    11/09/11 @ 07:38

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)