Solved

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

Posted on 2009-04-02
2
224 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
2 Comments
 
LVL 142

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

19 Experts available now in Live!

Get 1:1 Help Now