Solved

10774: alternatives to the OR sentence (dinamyc query)

Posted on 2014-11-20
7
110 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

739 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