SSRS Querying on either of 2 Parameters

The situation is that we are replacing a website-application with a single SQL Server Reporting Services (SSRS 2008 R2) report. We're using Business Intelligence Development Studio (BIDS VS2008) with Team Foundation Server (TFS VS2010) and connecting to an Oracle database (ie. "seamless integration because our setup is perfect and well thought through" not - note the DBMS is not hugely relevant for the purposes of this article).

The end-user must be able to search on EITHER the student's username or the student's ID (2 report parameters: @StudentADAccount [varchar] and @StudentReference [int] respectively). Most of the remaining datasets use the resulting @StudentReference number in their "where" clause. A student always has a "Student Reference" but not necessarily a student AD account (enquired/applied only).

So following the above, I end up with:
Initial Setup: 2 parameters

Then I want the ID parameter ("@StudentReference") to populate itself if it is left blank when the report is executed. So my first dataset has to include the Username check as well (…WHERE StudentID=@StudentReference OR StudentName=@StudentADAccount). And according to other web articles on this matter, I have to change the order of my parameters (I will be checking "Student Username" first as "Student Reference" must be calculated and not blank/null):
Corrected Setup: 2 parameters

So I tried setting the "Available Values" as suggested by Stack Overflow to gets its value based on the first parameter but ended up with an error going on about "forward dependencies".
Error: parameter has a value that depends on the other parameter.  Forward dependencies would be more useful than this rubbish...

This is obviously going to error, so continuing on their solution, setting the "Available Values – Specify values" to
=IIF(LTrim(Parameters!StudentADAccount.Value)="", Parameters!StudentReference.Value, First(Fields!S_STUDENTREFERENCE.Value, "StudentDetails"))
  1.  =IIF(LTrim(Parameters!StudentADAccount.Value)="", Parameters!StudentReference.Value, First(Fields!S_STUDENTREFERENCE.Value, "StudentDetails")) 
should work:
Error: Fields cannot be used in report parameter expressions

Apparently not because "fields cannot be used in report parameter expressions". I tried this with "Default values" and got the same problems. Note: before you try, "Variable values cannot be used in report parameter expressions" either.

I'm using an Oracle/PLSQL database for this so whether you're using T-SQL or MySQL you'll need to use the appropriate variable references (eg. T-SQL is @localvariable, Oracle is :localvariable). I need to change it to a conditional query with the OR statement below:

Lets take the following query as an example:
SELECT table1.studentID 
       , table2.studentUsername
FROM   table1, 
WHERE  table1.studentID = capd_table2.studentID 
       AND ( table1.studentID = :StudentReference 
              OR table2.studentUsername = :StudentADAccount )
  1.  SELECT table1.studentID 
  2.         , table2.studentUsername 
  3.  FROM   table1, 
  4.         table2 
  5.  WHERE  table1.studentID = capd_table2.studentID 
  6.         AND ( table1.studentID = :StudentReference 
  7.                OR table2.studentUsername = :StudentADAccount ) 

Now searching on the ID parameter ("@StudentReference") still works but when I type a Username ("@StudentADAccount"), it populates only the first dataset with results; the remaining datasets that need to use the ID parameter all returned zero rows.
  1. So one workaround exists where if I combined all 10 dataset queries into 1 mega dataset query, problem solved…
  2. Another workaround exists where if I added the OR part to all my datasets (so where each one says if studentreference is blank then compare to studentadaccount)…
Both solutions have the issue where they will return all rows where studentadaccount is blank (this field can be null in the database); so that's a lot of rows, possibly all unrelated and won't work in my particular case. The solution to this issue of these solutions would be to add another AND clause where you state that the ID (DB.Table.StudentReference) can NOT be blank/null. End-users won't usually search based on the username (@StudentADAccount) if it wasn't provided to start with. Additionally, the second solution will mean each dataset must at least join both tables that you are using the OR clause against.

There must be a solution that executes a query at report execution time and populates/assigns the local variable ID (@StudentReference) everytime. So after an hour or so I came across "Cascading Parameters" ( Maybe the answer could be in that mess of a Microsoft article but this I can already do and it didn't quite do what I wanted.

I ran out of time (all MORNING!) so I went with solution #2 (above) and added the OR clause to all my dataset queries and joined the table containing the other parameter (@StudentADAccount) in each dataset query, which slowed it down a little but the requirement was met. If your end-users complain, do what Microsoft suggests and throw more memory at the SQL Server.

The remaining issue is what if the user leaves both fields blank? This would return all users who don't have an AD account. Using the revisited solution below -> Just leave the "allow blank" option in the parameter properties unchecked

SSRS Report Revisted:

Had an issue where SSRS report parameters were not resetting to blank after the report was run. End-Users were putting different values in both fields and getting twice as many rows back. I modified the report leaving only one open parameter, the OR clause just compares against the same parameter.

So for example: Instead of
(student_accounts.student_name=@StudentName or student_accounts.student_id=@StudentID)
  1.  ... 
  2.  (student_accounts.student_name=@StudentName or student_accounts.student_id=@StudentID) 
I set this to
(student_accounts.student_name=@SearchWord OR student_accounts.student_id=@SearchWord)
  1.  ... 
  2.  (student_accounts.student_name=@SearchWord OR student_accounts.student_id=@SearchWord) 

Sorry only a workaround at the moment!

I don't think I can recommend Microsoft's SQL Server Reporting Services as a replacement for website applications. We only did it because we have more staff trained in SSRS than in bespoke third-party software and the ability to support/develop outweighed the cons.

Category: SQL Server Reporting Services :: Article: 370

Credit where Credit is Due:

Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman

Related Articles

Joes Revolver Map


Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Donate to Joel Lipman via PayPal

Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.