Reporting On Data From Stored Procedures (part 1)
November 10, 2009 in Reporting Services, SQLServerPedia Syndication | 7 comments
Introduction
The first step of creating a SQL Server Reporting Services (SSRS) report involves setting up a connection to the data source and programming a dataset to retrieve data from that data source. The dataset can use a SELECT query, which is the most common way of retrieving data and one that you’re probably already familiar with. But it can also use a Stored Procedure (aka stored proc or SP).
The purpose of this article is to demonstrate how data can be retrieved from a SQL Server database through Stored Procedures defined in that same database, and then displayed in a SQL Server 2008 Reporting Services report.
I’ll be using the AdventureWorks2008 sample database available for download at CodePlex.
What Are Stored Procedures?
There are actually different types of stored procedure in the context of SQL Server. The type that I am using in this article is called a “Transact-SQL Stored Procedure”. According to the Books Online, this type of stored procedure is:
“A saved collection of Transact-SQL statements that can take and return user-supplied parameters.”
If you have experience using a regular programming language such as Visual Basic or C#, I’m sure this sounds familiar. You can think of a stored procedure as a method that takes any number of parameters, depending on its definition, and that possibly returns a result, again depending on its definition. Instead of being stored in a compiled .exe or .dll, it is stored in the database.
In this article I will be using some stored procedures that return a dataset as result. How stored procedures are written is not the purpose of this article. For that I’d like to refer you to this Books Online page: Implementing Stored Procedures.
Reasons For Using A Stored Procedure
There are several reasons why it is more interesting to use datasets based on stored procedures as opposed to SELECT statements.
Performance: stored procedures perform faster than SELECT statements. The reason for this is because they are compiled when they’re created and their execution plan gets stored by SQL Server so that it can be reused for each procedure call.
Maintenance: when a database needs to undergo some changes to its schema, the changes can be handled in the stored procedures. This makes it transparent for the reports that are using these stored procedures. The reports will keep functioning as expected without any modifications to them.
Reuse: imagine a situation where several reports are reporting on the same set of data. If you wouldn’t use stored procedures, you may have to repeat a possibly complex query in each report. With stored procedures you just need to define the query in the stored procedure and then call it in each report’s dataset.
Security: in environments where DBAs are responsible for the SQL Server databases, the report developers will possibly not get sufficient rights to retrieve data from the tables directly. One of the ways to prevent this is to give them access to a bunch of stored procedures and views instead.
Simple Procedure Call
Okay, enough theory, time to show you how it’s done!
Setting Up The Stored Procedure
Our first procedure queries the AdventureWorks database to return a list of employees with their corresponding department. Here’s the code for the SP:
CREATE PROCEDURE GetEmployeeData AS BEGIN SELECT E.NationalIDNumber, E.JobTitle, E.BirthDate, E.MaritalStatus, E.Gender, E.HireDate, E.SalariedFlag, E.VacationHours, E.SickLeaveHours, D.GroupName as DepartmentGroupName, D.Name as DepartmentName, P.FirstName, P.MiddleName, P.LastName FROM HumanResources.Employee E INNER JOIN HumanResources.EmployeeDepartmentHistory EDH ON EDH.BusinessEntityID = E.BusinessEntityID AND EDH.EndDate IS NULL -- current active department does not have EndDate filled in INNER JOIN HumanResources.Department D ON D.DepartmentID = EDH.DepartmentID INNER JOIN Person.Person P ON P.BusinessEntityID = E.BusinessEntityID; END
Creating The Report
Creating A Shared Data Source
Creating The Data Source
Creating The Dataset
I’m calling the Dataset dsEmployeeData. The Data Source edit box is already prefilled with the right data source because we’ve right-clicked the data source to which we want to add a dataset, how easy can it be?
To query a stored procedure from a dataset is really very straightforward. All you need to do now is select the Stored Procedure radio button. Doing that replaces the bottom part of the window. Instead of an edit box that expects a query, we now get a simple dropdown where we need to select our SP.
Once you’ve done that, select the Fields page on the left.
As you can see, the list of fields has been pre-filled. The BIDS queries the stored procedure’s metadata so we do not need to manually specify the fields returned by our SP, saving us quite some time!
Once you click OK to close the Dataset Properties window, you’ll see that the Report Data pane gets populated with our list of fields.
Show Me The Data
To prove that everything is working as expected, let’s set up a quick report using a Table. From the Toolbox pane, drag a Table object onto the report canvas:
Next, from the Report Data pane, drag some fields into the cells in the Data part of the Table. As we’re dealing with employee data, it would be interesting to see their names. I’ve also added their job and department.
Resize the columns a bit so that everything will fit nicely, and color the Header cells different from the Data cells to easily distinguish them.
Now we’re ready to run the report, so click the Report’s Preview tab.
So, that was our first procedure. Wasn’t too complicated, was it? So, ready for another one?
Parameterized Procedure Call
This time we’ll make it a little more complicated. As you know, stored procedures can take parameters. And SSRS knows how to pass them into a stored proc. So let’s do that!
Setting Up Stored Proc Version 2.0
The stored procedure shown below is based on our previous one. Except now it takes one parameter: @HireDate. The SP will only return employees that have been hired after given HireDate.
ALTER PROCEDURE GetEmployeeData @HireDate date AS BEGIN SELECT E.NationalIDNumber, E.JobTitle, E.BirthDate, E.MaritalStatus, E.Gender, E.HireDate, E.SalariedFlag, E.VacationHours, E.SickLeaveHours, D.GroupName as DepartmentGroupName, D.Name as DepartmentName, P.FirstName, P.MiddleName, P.LastName FROM HumanResources.Employee E INNER JOIN HumanResources.EmployeeDepartmentHistory EDH ON EDH.BusinessEntityID = E.BusinessEntityID AND EDH.EndDate IS NULL -- current active department does not have EndDate filled in INNER JOIN HumanResources.Department D ON D.DepartmentID = EDH.DepartmentID INNER JOIN Person.Person P ON P.BusinessEntityID = E.BusinessEntityID WHERE E.HireDate > @HireDate; END
As the HireDate field in the HumanResources.Employee table is of type date, I chose to make the parameter that same data type.
Please note that the above script uses ALTER PROCEDURE instead of CREATE PROCEDURE. This will only work if you’ve already created the previous procedure. If not, just replace the word ALTER with CREATE.
Modify Report To Pass Parameter To Stored Procedure
The next step is to modify our existing Dataset so that it passes a date into our stored procedure.
Open up the Dataset’s properties by double-clicking it in the Report Data pane. Then select the Parameters page:
As you can see, it is still empty.
Now switch to the Query page and click the Refresh Fields… button:
Then switch back to the Parameters page:
The parameter has been automatically added when we clicked the Refresh Fields… button!
The BIDS again used the stored proc’s metadata to do this. No need for manual intervention. For now, leave the Parameter Value box as it is, empty. Click OK to close the properties window.
Clicking OK caused another action in our report! If you open up the Parameters folder in the Report Data pane, you’ll see that it has gotten an item as well:
You can double-click the @HireDate parameter to get its properties:
The BIDS has created a report parameter and linked it to the parameter in our Dataset. It automatically chooses the correct data type for the report parameter, Date/Time in this case. (Unlike in T-SQL, there are no separate Date and Time data types in SSRS 2008.)
To confirm that the report parameter is linked to the parameter in the Dataset, close the Report Parameter Properties window and open up the Dataset Properties again, selecting the Parameters page:
The Parameter Value for our parameter has now gotten a value, more precisely it’s referring to the Report Parameter called @HireDate. If you don’t believe that it is actually the report parameter (I admit, it looks similar to the parameter in the Dataset, except for the square brackets surrounding the report parameter’s name.), click the fx button to see the formula:
Indeed, it is referring to the parameter on the report.
Test The Changes
Now that the parameter has been set up, it’s time to test our report again.
As the report is now filtering on the HireDate, it would be interesting to actually show this field in the table. Add an extra column and drag this field into the data cell.
Our newly added field’s type is datetime, but we’re only interested in the date part. So we’ll set up a format code. Select the HireDate data cell and locate the Format property in the Properties pane. Enter a small d into it:
This instructs the BIDS to show the field using the “short date pattern”. A list of possible codes is available on MSDN: Standard Date and Time Format Strings. As you notice, SSRS is using the regular formatting strings as they are known in .NET.
Note also that the format used to display the data depends on the report’s Language property. I’ll leave it at its default: en-US.
Right, time to have a look at the report. Select the Preview tab. You can now see the Hire Date parameter which is expecting a value. You can either type a value or select it from the calendar which is shown when you click the button on the right of the textbox:
To be sure that you’re using the correct format for the date, I suggest to select a date using the control. Then you can always modify it to whatever day you’d like to select.
For our test I know that some employees started before 2000 and others later, so I’ll select a date somewhere in the year 2000.
And indeed, our report shows only employees that started after our selected HireDate.
Conclusion
With this article I hope to have shown you that it’s fairly straightforward to report on data coming from stored procedures.
In case the above hasn’t fulfilled your appetite on this subject yet, watch out for part 2. In that sequel I will build further on the example used in this article to show you how you can use multi-value parameters to filter your report’s data.
In the meantime: happy reporting, and thank you for reading my article!
References
BOL 2008 – How to: Create an Embedded or Shared Data Source
BOL 2008 – How to: Create a Dataset (Reporting Services)
BOL 2008 – How to: Refresh Fields for a Dataset
Tags: data, Reporting Services 2008, SSRS, Tutorial
-
Sid on July 29, 2010 at 3:17 PM
Thanks for this — I’m finally back into SSRS after a long time.
I’m finding problems using the report designer
Report Designer in 2008 SSRS R2 times out when adding a dataset which calls a long running stored procedure. Error Message:Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
I can tell that when adding a stored procedure SSRS report designer is calling the proc.
I have captured the trace on SQL profiler and I can see the procedure call.
The stored proc on it’s own takes just over one minute to complete execution.
Is there anyway to increase (looks like 30 seconds is the default) the time out when the report designer executes this proc call when designing the report?
Thanks,
Sid-
Valentino Vranken on October 14, 2010 at 7:54 AM
Hi Sid,
(Sorry for not getting back to you sooner, the notification must have gotten lost in the bunch of emails during my holidays.)
I’m not aware of a setting that would influence this. But what I would probably do in such a situation is to change the slow SP. The best change would be to rewrite it so that its speed is acceptable. When that is not an option due to the specific situation, I would temporarily add a SELECT statement at the top to return some dummy records. Obviously the fields returned should match with what would be returned in normal circumstances. Then once development is finished you can put the original SP back on the server.
Best regards,
Valentino.
-
-
Frank on October 13, 2010 at 10:04 PM
I am new to SSRS. This article was extremely helpful in getting my first report (with a paramterized stored procedure) to execute. Thanks for providing this information.
-
Edmond on May 4, 2011 at 11:30 PM
I just started learning about SSRS and I am extremely thankful for the step-by-step process that was posted. THANKS again.
-
rakesh on August 25, 2011 at 9:08 AM
nice really this blog helped me out,thanx
i will take ur help for further queries
Awards

Certification



Recent Posts
- SSIS: Highlight Unused Package Variables
- Posting An Issue To Microsoft Connect
- Where’s My SSIS Toolbox?!
- The Funny SSIS Container
- Enhanced Duplicate Key Error Message
- Two Weeks, Two SQL Server Conferences
- SQL Server Days 2011: Not Too Late (Yet)!
- SSMS: Saving Changes Not Permitted
- SQL Server Days 2011: Registration Open!
- Book Deal: 10 Days Of Microsoft @ Packt Publishing
Tags
Categories
Archives
- January 2012 (2)
- December 2011 (2)
- November 2011 (2)
- October 2011 (1)
- September 2011 (3)
- August 2011 (2)
- June 2011 (2)
- May 2011 (3)
- April 2011 (3)
- March 2011 (3)
- February 2011 (2)
- January 2011 (5)
- December 2010 (1)
- November 2010 (3)
- October 2010 (3)
- September 2010 (2)
- August 2010 (4)
- July 2010 (2)
- June 2010 (4)
- May 2010 (6)
- April 2010 (3)
- March 2010 (3)
- February 2010 (11)
- January 2010 (9)
- December 2009 (2)
- November 2009 (3)
- October 2009 (3)
- September 2009 (4)
- August 2009 (6)
- July 2009 (2)
- June 2009 (3)
- May 2009 (7)
- April 2009 (3)
- March 2009 (3)
- February 2009 (5)
- January 2009 (4)
- December 2008 (2)
- November 2008 (3)
- October 2008 (1)
- September 2008 (1)
- August 2008 (4)
- July 2008 (3)
Recommended Reading
- Just Announced - SQL Server 2012 - coming to you in 2012 October 11, 2011 Dandy Weyn
- How to Install Master Data Services (MDS) Service Pack 1 (for SQL Server 2008 R2) August 16, 2011 mattande
- Expression Adorners August 13, 2011 Matt Masson - MSFT
- Report Authoring on the SSIS Catalog August 1, 2011 Matt Masson - MSFT
- Managing SSIS Projects through SSMS July 20, 2011 mmasson
- Flat File Source Changes in Denali July 17, 2011 mmasson
- Overview of the DQS Cleansing Transform July 14, 2011 mmasson
- SQL Server codename "Denali" CTP3, including Project "Crescent" is now publically available July 12, 2011 Thierry Dhers
- SQL Server Code Name “Denali” CTP3 and SQL Server 2008 R2 SP1 are HERE! July 12, 2011 SQL Server Team
- Fixing SQL Server Management Studio’s Tab Text June 7, 2011 Brent Ozar
Tools You Really Need
Service Packs
SQL Server Material
- Common Solutions for T-SQL Problems
- Microsoft IT Showcase
- Microsoft Learning
- SQL Server 2008 Community Articles
- SQL Server 2008 MCM Readiness Videos
- SQL Server Books Online
- SQL Server Community Projects & Samples
- SQL Server Customer Advisory Team
- SQL Server Homepage
- SQL Server Library
- SQL Server TechCenter


7 comments
Comments feed for this article
Trackback link: http://blog.hoegaerden.be/2009/11/10/reporting-on-data-from-stored-procedures-part-1/trackback/