Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

10774: alternatives to the OR sentence (dinamyc query)

Posted on 2014-11-20
7
Medium Priority
?
113 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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 600 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1400 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 70

Accepted Solution

by:
Scott Pletcher earned 1400 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

721 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