Using Parameters in SSRS to Drive Stored Procedures

By Tom Nonmacher

Welcome to the latest SQLSupport.org blog post! Today, we delve into the exciting world of SQL Server Reporting Services (SSRS) to discuss how parameters can be used to drive stored procedures. This technique is crucial for creating flexible, dynamic reports that can adapt to user input and requirements. It is applicable across a range of technologies, including SQL Server 2019, MySQL 8.0, DB2 11.5, Azure SQL, and Azure Synapse.

To start with, a parameter in SSRS is a user-defined value that can be passed to a report at runtime. This allows the user to control report data, change report presentation, and conditionally control report processing. On the other hand, a stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again. Hence, SSRS parameters can be mapped to stored procedure parameters to retrieve the precise data required for your report.

For SQL Server 2019, you can create a stored procedure that accepts parameters and then, in your SSRS report, map report parameters to the stored procedure parameters. Here is an example of such a stored procedure:

CREATE PROCEDURE GetOrders @CustomerId INT AS
SELECT * FROM Orders WHERE CustomerId = @CustomerId
GO

For MySQL 8.0, the stored procedure creation process is similar, and so is the mapping of SSRS parameters. Here is how you can create a stored procedure in MySQL:

DELIMITER $$
CREATE PROCEDURE GetCustomerOrders(IN customerId INT)
BEGIN
SELECT * FROM Orders WHERE CustomerId = customerId;
END $$
DELIMITER ;

In DB2 11.5, the process is quite similar, though the syntax varies somewhat. Here's an example of a DB2 stored procedure:

--#SET TERMINATOR @
CREATE PROCEDURE GetCustomerOrders(IN customerId INT)
LANGUAGE SQL
BEGIN
SELECT * FROM Orders WHERE CustomerId = customerId;
END @
--#SET TERMINATOR ;

SQL Server on Azure and Azure Synapse Analytics also support stored procedures and SSRS parameters. The process of creating stored procedures and mapping parameters is the same as in SQL Server 2019. Thus, the knowledge is easily transferable if you're migrating from on-premises SQL Server to Azure.

In conclusion, using parameters in SSRS to drive stored procedures is a powerful technique that allows for the creation of flexible, dynamic reports. This broadens the utility of SSRS and allows for a greater level of interactivity and customization in reports, irrespective of whether you're using SQL Server 2019, MySQL 8.0, DB2 11.5, Azure SQL, or Azure Synapse.




B9620B
Please enter the code from the image above in the box below.