Explain Codes LogoExplain Codes Logo

Passing multiple values for a single parameter in Reporting Services

sql
string-split
custom-functions
sql-injection
Alex KataevbyAlex Kataev·Aug 20, 2024
TLDR

To accommodate multiple values for a single parameter in SQL Server Reporting Services (SSRS), you can form a delimited string using the JOIN function in the report's parameter and then split these values in the SQL query using STRING_SPLIT.

Example:

  1. Configure the SSRS parameter to receive multiple values and join them:

    =Join(Parameters!YourParameter.Value, ",")
  2. Update the SQL query to accommodate the parameter and split it:

    SELECT * FROM YourTable WHERE YourColumn IN (SELECT value FROM STRING_SPLIT(@YourParameter, ','))

By passing them as a concatenated string to the SQL query, multiple values within a single parameter are smoothly handled and parsed into individual values.

Simple filtering using IN and STRING_SPLIT

The IN clause and the STRING_SPLIT function work well together to provide a straightforward way to filter information based on multiple parameter values. Specifically tailored to web-based queries, you can encode these values prior to transmission, then decode on the server side to safeguard data integrity and security.

Example:

//Web Query String https://yourserver/report?params=1,2,3,4
//Server-side decoding SELECT * FROM YourTable WHERE YourColumn IN (SELECT value FROM STRING_SPLIT(@YourParameter, ','))

Ensure your database compatibility level supports this to avoid unexpected issues.

Handling complex types with custom functions

You may encounter a situation where STRING_SPLIT isn't able to fulfill your needs due to complex data types or when encountering non-VARCHAR values. Implementing a custom function, for instance fn_SplitInt for integer arrays, offers a solution in these cases:

-- Assuming @YourParameter is '1,2,3' and your official DJ for this query run SELECT * FROM YourTable WHERE Id IN (SELECT value FROM dbo.fn_SplitInt(@YourParameter, ',')) -- Let the good times roll!

For more intricate data types, you can cast these values within your custom split function, or manipulate them to match your needs prior to comparison.

Taming the wild Parameters tab

In your SSRS report, the Parameters tab of your dataset is where you tame the behavior and properties of your parameters. Here you set up the JOIN function for your multiple choice companionship. Make sure to check "Allow multiple values" and set up "Available values" properly to offer the user a polished and intuitive experience.

Points to ponder for robust solutions

Handling eccentric citizens

Consider edge cases where parameters roll in with special characters, empty strings, or nulls. Your validation logic should be prepped and ready for these curves, ensuring reports are generated without mistakes and are user-approved.

Making room for data expansion

When your datasets grow larger than an elephant, this method could start to resemble a tortoise. For such scenarios, formulate strategies to optimize your query. This could mean dabbling with indexes, temporary tables, or even pre-processing parameters in a stored procedure before the main event.

Guarding the fortress

Interestingly, parameters aren't just data—they could also be nasty SQL injection vectors. Scrub user inputs clean and handle parameter values with caution. Deploying parameterized queries or stored procedures enhances your security shield.No villains allowed inside!