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
copyraw
=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:
copyraw
SELECT table1.studentID 
       , table2.studentUsername
FROM   table1, 
       table2 
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" (http://technet.microsoft.com/en-us/library/dd255197.aspx). 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
copyraw
...
(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
copyraw
...
(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: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used 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
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

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:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.