Solved

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

Posted on 2009-04-02
2
213 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

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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…
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, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

8 Experts available now in Live!

Get 1:1 Help Now