Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SSRS Multil Select Parameter - Query Help

Posted on 2014-11-12
8
Medium Priority
?
250 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
ID: 40437912
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
ID: 40437948
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
ID: 40437951
How do I change my query so either can be used but not both.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40437955
Which is your preference?
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 2000 total points
ID: 40437963
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
ID: 40437976
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
ID: 40437997
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
ID: 40438143
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
In this article I will describe the Copy Database Wizard 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.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

971 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