Solved

10774: alternatives to the OR sentence (dinamyc query)

Posted on 2014-11-20
7
104 Views
Last Modified: 2014-11-22
Hi experts:

I have a store procedure that can take 10 parameters. The user can enter 1,2,3 to 10 parameters. Do it OR is very expensive, can show T-SQL code to solve

SELECT
  *
  FROM TSQL2012.Production.Suppliers
  where city = @city OR phone = @phone OR contacttitle = @contacttitle OR country=@country
   ............. parametro10 = @parametro10
0
Comment
Question by:enrique_aeo
  • 3
  • 2
  • 2
7 Comments
 
LVL 39

Expert Comment

by:lcohan
Comment Utility
Ever thought a UNION may work better? it implies DISTINCT so no duplicate rows will exists.
Or INTERSECT to get the row "identifier" in a CTE for instance than use that limited CTE record set to JOIN with actual table and get the rest of the columns
0
 

Author Comment

by:enrique_aeo
Comment Utility
Give me code T-SQL please
0
 
LVL 39

Expert Comment

by:lcohan
Comment Utility
SELECT * FROM TSQL2012.Production.Suppliers
WHERE city = @city
UNION
SELECT * FROM TSQL2012.Production.Suppliers
WHERE phone = @phone
UNION
SELECT * FROM TSQL2012.Production.Suppliers
WHERE contacttitle = @contacttitle
UNION
SELECT * FROM TSQL2012.Production.Suppliers
WHERE country= @country

And make sure you have an index on each of the above columns

And then you build your dynaminc SQL by not having in it the
...
UNION
SELECT * FROM TSQL2012.Production.Suppliers
WHERE country= @country
...

statement(s) where parameter value passed in IS NULL

and you execute the dynamic string by calling SP_EXECUTESQL
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Assisted Solution

by:lcohan
lcohan earned 150 total points
Comment Utility
Or here - I think this is even better because it does not require dynamic SQL as the IS NOT NULL is included on each "branch" of the query. So all you need is pass NULL from app to the parameters you don't want/need in the SP and that particular select will return noting.



SELECT * FROM TSQL2012.Production.Suppliers
WHERE city = @city and @city IS NOT NULL
      UNION
SELECT * FROM TSQL2012.Production.Suppliers
WHERE phone = @phone and @phone IS NOT NULL
      UNION
SELECT * FROM TSQL2012.Production.Suppliers
WHERE contacttitle = @contacttitle  and @contacttitle IS NOT NULL
      UNION
SELECT * FROM TSQL2012.Production.Suppliers
WHERE country= @country and @country IS NOT NULL
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 350 total points
Comment Utility
You really must use dynamic SQL to get good performance from these types of generic queries.  You can use sp_executesql, rather than EXEC, to drastically reduce the chance of SQL injection.
0
 

Author Comment

by:enrique_aeo
Comment Utility
ScottPletcher:

can you give me an example dynamic SQL
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 350 total points
Comment Utility
Here's the simplest method, although you'll want to add checks to prevent SQL injection using this method:


DECLARE @sql varchar(max)
SET @sql = '
SELECT *
FROM TSQL2012.Production.Suppliers
WHERE 1 = 1'
IF @city > ''
    SET @sql = @sql + '
    AND (@city = city)'
IF @phone > ''
    SET @sql = @sql + '
    AND (@phone = phone)'
IF @contracttitle > ''
    SET @sql = @sql + '
    AND (@contacttitle = contacttitle)'
--IF ...other_parms...
    --SET ... + ...

EXEC (@sql)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

762 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

10 Experts available now in Live!

Get 1:1 Help Now