Solved

Make a query return all results when no WHERE condition is specified

Posted on 2009-04-02
2
245 Views
Last Modified: 2012-05-06
I have the following query.

DECLARE @ReportType VARCHAR(255)
DECLARE @FilterValue VARCHAR(255)

DECLARE @VO VARCHAR(255)
DECLARE @BU VARCHAR(255)
DECLARE @SG VARCHAR(255)

IF @ReportType = 'VO' BEGIN SET @VO = @FilterValue END
IF @ReportType = 'BU' BEGIN SET @BU = @FilterValue END
IF @ReportType = 'SG' BEGIN SET @SG = @FilterValue END

SELECT  VValues.VVal
FROM VValues
INNER JOIN Assets ON VValues.GUID = Assets.GUID
INNER JOIN Contacts ON Assets.BusID = Contacts.BusID
WHERE 1 = case when @v1 is not null and Contacts.ContactName = @v1 then 1                
when @v2 is not null and Assets.AssetOwner = @v2 then 1
when @v3 is not null and Contacts.SubGroup = @v3 then 1
else 0 end

Which after much help and debate (see == >> http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/Q_24281757.html for history) works flawlessly. However, I would now like to enhance it so that not only will it dynamically filter by the value that I pass in but negate all filters if a " * " is passed as @FilterValue.  Thanks in advance on this one..
0
Comment
Question by:jclemo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24055592
this should do

SELECT VValues.VVal
FROM VValues
INNER JOIN Assets ON VValues.GUID = Assets.GUID
INNER JOIN Contacts ON Assets.BusID = Contacts.BusID
WHERE 1 = case when @filtervalue = '*' then 1
when @v1 is not null and Contacts.ContactName = @v1 then 1
when @v2 is not null and Assets.AssetOwner = @v2 then 1
when @v3 is not null and Contacts.SubGroup = @v3 then 1
else 0 end

Open in new window

0
 

Author Closing Comment

by:jclemo
ID: 31566050
works like a charm... thanks..
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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