Reporting On Data From Stored Procedures (part 1)
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.
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!
- Silly SQL #1: OLE DB Destination [SSIS]
- Deploying PDFs, and more [SSRS]
- T-SQL Tuesday 50: Automation, Automation, Automation!
- The "Select ALL" parameter option [SSRS]
- Book: SQL Server 2012 Reporting Services Blueprints
- SQL Server Days 2013 – Data Cleansing: Download
- SQL Server Days 2013
- Exploring the System.Object Package Variable [SSIS]
- T-SQL Tuesday 46: Contraptions!
- Creating Multi-Column Reports: The Top-Down Version [SSRS]
- August 2014 (1)
- May 2014 (1)
- January 2014 (1)
- December 2013 (1)
- November 2013 (2)
- October 2013 (1)
- September 2013 (2)
- July 2013 (2)
- June 2013 (2)
- May 2013 (3)
- March 2013 (3)
- February 2013 (2)
- January 2013 (2)
- December 2012 (2)
- November 2012 (3)
- October 2012 (2)
- August 2012 (2)
- July 2012 (2)
- June 2012 (2)
- May 2012 (2)
- April 2012 (3)
- March 2012 (4)
- February 2012 (4)
- 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)