Solved

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

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Union 2 queries to a cte (temp table perhaps) 9 41
How to count the days a record spends in a step 21 51
SQL Syntax 24 46
shrink table after huge delete 2 15
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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