Solved

SSRS Multil Select Parameter - Query Help

Posted on 2014-11-12
8
212 Views
Last Modified: 2014-11-12
So I have an SSRS report that has 5 parameters.  Three of the parameters are set - with AND in the SQL Where clause:
  WHERE LossDate between @FromDate and @ToDate
  AND co.FileName like '%' + @Phrase + '%'

I have 2 other parameters that I need to add to the query and they are both optional

Here is my broken query:
SELECT c.LossDate, co.CorrespondenceID, c.ClaimNumber,
co.[FileName], co.Title, co.EntryDate, i.InsuredID, i.InsuredName1 as ClientName,
U1.LastName+', '+U1.FirstName as EnteredByName,
U2.LastName+', '+U2.FirstName ExaminerName,
AddressBook.LastFirstName as ClaimantName

FROM Correspondence co (NOLOCK)
JOIN Claimant cl (NOLOCK) ON co.ClaimantID = cl.ClaimantID
JOIN Claim c (NOLOCK) ON cl.ClaimID = c.ClaimID
JOIN Policy p (NOLOCK) ON c.PolicyID = p.PolicyID
JOIN Insured i (NOLOCK) ON p.InsuredID = i.InsuredID
LEFT OUTER JOIN CorrespondenceStatus cs (NOLOCK) ON co.StatusID = cs.StatusID
LEFT OUTER JOIN Users U1 (NOLOCK) ON co.EnteredBy = U1.UserName
LEFT OUTER JOIN Users U2 (NOLOCK) ON c.ExaminerCode = U2.UserName
LEFT OUTER JOIN AddressBook on cl.AddressBookID = AddressBook.AddressBookID

WHERE LossDate between @FromDate and @ToDate
AND co.FileName like '%' + @Phrase + '%'
AND (ClientName in(@ClientName) OR @ClientName IS NULL)
AND (ClaimNumber = @ClaimNumber OR @ClaimNumber IS NULL)

@ClientName  is a list box where they can select one or all values - with no default value
@ClaimNumberis a text parameter where they can enter a single value

The issue with them both being AND in the Where clause is if a user enters a Client and then enters a ClaimNumber that doesn't match, the report won't work.

Do I need to use a Case statement in the Where clause?
0
Comment
Question by:Scott Williams
  • 5
  • 3
8 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
My thoughts:

I can't see ClientName in your SELECT.

Why doesn't the report work - what do you mean? You get no results?

>> if a user enters a Client and then enters a ClaimNumber that doesn't match, the report won't work.

Yep - that is what would happen. So what do you want to happen instead?
0
 

Author Comment

by:Scott Williams
Comment Utility
i.InsuredName1 as ClientName

I have corrected my Where statement to this:
WHERE LossDate between @FromDate and @ToDate
AND co.FileName like '%' + @Phrase + '%'
AND (i.InsuredName1 in(@ClientName) OR @ClientName IS NULL)
AND (ClaimNumber = @ClaimNumber OR @ClaimNumber IS NULL)

So If I leave the ClientName blank and set the ClaimNumber =GCGL01005724
I get a Parameter Error - asking me to select a value for ClientName
0
 

Author Comment

by:Scott Williams
Comment Utility
How do I change my query so either can be used but not both.
0
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
Which is your preference?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
Comment Utility
If you have this:

AND (ClientName in(@ClientName) OR @ClientName IS NULL)
AND (ClaimNumber = @ClaimNumber OR @ClaimNumber IS NULL)

and you don't want the ClaimNumber to be considered if there is a ClientName, then change it to this:

AND (ClientName in(@ClientName) OR @ClientName IS NULL)
AND (ClaimNumber = @ClaimNumber OR @ClaimNumber IS NULL OR @ClientName IS NOT NULL)
0
 

Author Comment

by:Scott Williams
Comment Utility
So when I use this:
  AND (ClientName in(@ClientName) OR @ClientName IS NULL)
  AND (ClaimNumber = @ClaimNumber OR @ClaimNumber IS NULL OR @ClientName IS NOT NULL)

I run the report and enter the From and To Date and the Claim Number and leave the ClientName empty and I get error - Select a value for parameter ClientName
0
 

Author Comment

by:Scott Williams
Comment Utility
So then I tried to run with the Claim Number set to GCGL01005724 and the ClientName set to All and I get this error:
An expression of non-boolean type specified in a context where a condition is expected, near ','.

Here's my query:
SELECT c.LossDate, co.CorrespondenceID, c.ClaimNumber,
co.[FileName], co.Title, co.EntryDate,
U1.LastName+', '+U1.FirstName as EnteredByName,
U2.LastName+', '+U2.FirstName ExaminerName,
AddressBook.LastFirstName as ClaimantName,
i.InsuredID, i.InsuredName1

FROM Correspondence co (NOLOCK)
JOIN Claimant cl (NOLOCK) ON co.ClaimantID = cl.ClaimantID
JOIN Claim c (NOLOCK) ON cl.ClaimID = c.ClaimID
JOIN Policy p (NOLOCK) ON c.PolicyID = p.PolicyID
JOIN Insured i (NOLOCK) ON p.InsuredID = i.InsuredID
LEFT OUTER JOIN CorrespondenceStatus cs (NOLOCK) ON co.StatusID = cs.StatusID
LEFT OUTER JOIN Users U1 (NOLOCK) ON co.EnteredBy = U1.UserName
LEFT OUTER JOIN Users U2 (NOLOCK) ON c.ExaminerCode = U2.UserName
LEFT OUTER JOIN AddressBook on cl.AddressBookID = AddressBook.AddressBookID

WHERE LossDate between @FromDate and @ToDate
AND co.FileName like '%' + @Phrase + '%'
AND (InsuredName1 in(@ClientName) OR @ClientName IS NULL)
AND (ClaimNumber = @ClaimNumber OR @ClaimNumber IS NULL OR @ClientName IS NOT NULL)
0
 

Author Comment

by:Scott Williams
Comment Utility
Here's what I want to do.  But it won't work:

WHERE CASE WHEN ISNULL(@ClaimNumber) THEN InsuredName1 in(@ClientName)
ELSE ClaimNumber = @ClaimNumber AND InsuredName1 in(select distinct InsuredName1 from Insured (NOLOCK)) END
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now