Solved

10774: alternatives to the OR sentence (dinamyc query)

Posted on 2014-11-20
7
109 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 40

Expert Comment

by:lcohan
ID: 40455779
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
ID: 40455788
Give me code T-SQL please
0
 
LVL 40

Expert Comment

by:lcohan
ID: 40455814
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 150 total points
ID: 40455851
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:Scott Pletcher
Scott Pletcher earned 350 total points
ID: 40456110
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
ID: 40456265
ScottPletcher:

can you give me an example dynamic SQL
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 350 total points
ID: 40456280
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

749 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