IT Nota

  • Home
  • How To
  • .NET
  • WordPress
  • Contact

How to Create SSRS Report with Dynamic Query and Multi-Value Parameters

Continuing from the last SSRS tutorial on creating a SQL Server Reporting Services report with dynamic query and parameters, one caveat about the report is that it only works for one record search at a time.

While this may be fine for a customer who needs to search a particular record specific to the person, this is not generally useful for office use, where one person may need to search possibly hundreds or thousands of records that will be exported to an excel spreadsheet for further scrubbing and presentation.

We want to enable users to enter several search keywords and have the report rendered if any matched records.

In order to accomplish this, we need to modify the Parameters to allow multiple values. If we go to the left pane on the Report Designer and open the @SearchString Parameter Properties, under General tab, click on the Allow multiple values check box and click OK.

SSRS Multiple Values Parameter Checkbox

What this does is to change the Search for: text box to a drop-down list box to allow search based on multiple keywords. However, the report can still only search for one value. If we entered two values as in the example below, no records will be retrieved because the search keyword is actually a concatenated value of those two words separated by comma (i.e., ‘brown,baker’).

SSRS Multiple Values Search (Preview)

The concatenation is done using =Join(Parameters!SearchString.Value) built-in function. SSRS is smart enough to do this automatically when you selected Allow multiple values on the Parameter Properties. While the report is stored enough to concatenate the multiple values, the linked stored procedure still doesn’t know that it’s supposed to split the parameters into several individual values.

To do this, we need to create a user-defined function (UDF) that will split the concatenated parameters as a one column table.

Create a Split Function

CREATE FUNCTION dbo.ufnSplit (
  @Input NVARCHAR(MAX), 
  @Delimiter CHAR(1) = ','
)
RETURNS @Values TABLE (Item NVARCHAR(100))
AS
BEGIN
  DECLARE @Pos INT
  DECLARE @Single NVARCHAR(100)
  SELECT @Pos = 1 
  WHILE @Pos > 0
    BEGIN
      SELECT @Pos = CHARINDEX(@Delimiter, @Input)
      IF @Pos > 0
        SELECT @Single = LEFT(@Input, @Pos - 1)
      ELSE
        SELECT @Single = @Input
      INSERT  @Values(Item) VALUES (@Single)
      SELECT @Input = RIGHT(@Input, LEN(@Input) - @Pos)
      IF LEN(@Input) = 0 BREAK
    END
  RETURN
END 

Implement Split Function

Next, we implement the split function in the dbo.SearchEmployee stored procedure created in previous posting.

Modify stored procedure dbo.SearchEmployee by using ALTER PROCEDURE and replace the last line of the SQL statement from

= (@SearchString)

To

IN (SELECT Item FROM dbo.ufnSplit(@SearchString, ','))

The full SQL statement should look like this:

ALTER PROCEDURE dbo.SearchEmployees 
	@SearchBy NVARCHAR(MAX),
	@SearchString NVARCHAR(MAX)
AS
SELECT NationalIDNumber, FirstName, MiddleName, LastName,
       JobTitle, LoginID 
FROM HumanResources.Employee e
INNER JOIN Person.Person p
ON e.BusinessEntityID = p.BusinessEntityID
WHERE 
	CASE 
		WHEN @SearchBy = 'IDNo' THEN NationalIDNumber
		WHEN @SearchBy = 'Title' THEN JobTitle
		WHEN @SearchBy = 'First' THEN FirstName
		WHEN @SearchBy = 'Middle' THEN MiddleName
		WHEN @SearchBy = 'Last' THEN LastName
		WHEN @SearchBy = 'Login' THEN LoginID
	END
	IN (SELECT Item FROM dbo.ufnSplit(@SearchString, ','))

Execute the SQL to save the stored procedure (or press F5) and you’re done.

Once the stored procedure is modified, it will know how to parse the multi-value parameters and pass back the results as shown in the screenshot below.

SSRS Multiple Values Search

Please share this post if you find it helpful.

Futher Reading

Microsoft SQL Server 2012 Reporting Services (Developer Reference)
code-u-like: SSRS multi-value parameters with less fail
Passing multiple values for a single parameter in Reporting Services
User-Defined Functions
Using Parameters Collection References in Expressions (Reporting Services)
Single-Value and Multivalue Parameters (Report Builder and SSRS)

July 22, 2013 Filed Under: How To Tagged With: SQL, SSRS

Buy me a coffee?

Buy me a coffee If you find any of the articles or demos helpful, please consider supporting my work here, you'll have my big thanks!

Buy me a coffee Support this site
Buy Me a Coffee?

Categories

  • .NET
  • Coding
  • Cybersecurity
  • Database
  • How To
  • Internet
  • Multimedia
  • Photography
  • Programming
  • Resources
  • Review
  • Tips and Tricks
  • Uncategorized
  • Use Case
  • WordPress
  • Writing

Recent Posts

  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency
  • How to Search for a String in All Tables in a Database

Recent Posts

  • How to View Stored Procedure Code in SQL Server
  • How to Find a String in SQL Server Stored Procedures
  • How to Remove Cached Credentials without Rebooting Windows
  • ESP Work Automation: Empowering Enterprises with Streamlined Workflows and Operational Efficiency
  • How to Search for a String in All Tables in a Database

Tags

.NET .NET Core AdSense ASP.NET Cdonts Dll Classic ASP Code Editor ETL FSharp Genesis Framework Git Google HP Asset Manager HTML5 Hugo IIS Information Security Internet Internet Information Services iOS JAMStack Linux macOS Microsoft Microsoft SQL Server MVC PHP PowerShell Python Simple Mail Transfer Protocol Smtp Server SQL SQL Server SSIS SSMS SSRS Sublime Text Visual Studio Visual Studio Code VPN Windows Windows 8 Windows 10 Windows 2012 Windows Server

Copyright © 2011-2025 IT Nota. All rights reserved. Terms of Use | Privacy Policy | Disclosure