Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

SQL Server Reporting Services (SSRS) – Exec SQL from a passed parameter

Let’s say you have a report that executes a dataset which produces fields for your report. You define that dataset like this:

select firstname, lastname, middlename 
from name_table

You then pull the columns from that table and load the data into your report fields (First Name, Last Name, and Middle Name).

Now, let’s say you want to make the SQL more complex by adding filtering with a WHERE statement like this:

select firstname, lastname, middlename 
  from name_table 
 where lastname = 'DOE'

Now comes the tricky part.

Let’s say that the report is executed from an application that has a search form. That form has 25 controls on it used for filtering. The client enters or chooses their filtering from those 25 controls and clicks a search button. The results are returned to a grid. The application then provides a PRINT button that executes your SSRS report that displays the same data provided in their grid.

You can do this two ways.

1) Define 25 parameters in your report and have your dataset interrogate each parameter in your WHERE clause. But what if every month, the application adds or removes controls from their search form? Now you have to change the report too!

2) Have the application pass the SQL statement used to get and populate their grid as a parameter to your report. You then define that one parameter in your report like this:

1

Then in your Dataset Properites, you must do two things:

a) In the Query Tab, inside the Query textbox, use the EXEC statement to execute the parameter that contains the SQL like this:

2

b) And lastly, you must manually define the fields in the Fields tab that are needed for the report like this:

3

As long as the data displayed in your report (See below) never changes, although the filtering in the application may change, you won’t need to change your report again. 🙂

4

Leave a Reply