short circuiting dynamic sql queries

Posted on 2008-11-17
Medium Priority
Last Modified: 2012-05-05

I'm in a bit of a jiffy here...

I'm trying to short-cicuit a sql query here, where if the value in the where clause is null then it'll query all records else, query only the values in the where clause.

here's my code...

declare @filter as varchar(50)
declare @SQLCmd as varchar(4000)

set @filter = 'xxxx' -
set @SQLCmd =  'select * from table
      where  NOT ('+@filter+' IS NOT NULL and table.field <> '+@filter+' )'
exec (@SQLCmd)      

this code works when @filter contains values on every executing. my problem is when i try to execute the code where @filter = null, @SQLCmd is null all in all..
ideally, the query should look like this:

select * from table
      where  NOT (null IS NOT NULL and table.field <> null )
can someone provide me with some tips?
subsequently, how do i make this work using a where... in clause?

Question by:praetorean
  • 2
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 22974073
You dont really need dynamic sql here

declare @filter as varchar(50)
set @filter = 'xxxx' -
select * from table
where (@Filter IS NULL or  field like '%'+@Filter+'%' )


Accepted Solution

praetorean earned 0 total points
ID: 22994158
i got it already... what i did was validate the value of the parameter... if = null then, i bypass and not add an additional where clause. if <> null, then i add an additional where clause...
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 22994349
if there is only one filter then this will be better

IF @Filter is Null
     select * from table
     select * from table where ield like '%'+@Filter+'%'

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

864 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