enrique_aeo
asked on
10774: alternatives to the OR sentence (dinamyc query)
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.Suppli ers
where city = @city OR phone = @phone OR contacttitle = @contacttitle OR country=@country
............. parametro10 = @parametro10
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.Suppli
where city = @city OR phone = @phone OR contacttitle = @contacttitle OR country=@country
............. parametro10 = @parametro10
ASKER
Give me code T-SQL please
SELECT * FROM TSQL2012.Production.Suppli ers
WHERE city = @city
UNION
SELECT * FROM TSQL2012.Production.Suppli ers
WHERE phone = @phone
UNION
SELECT * FROM TSQL2012.Production.Suppli ers
WHERE contacttitle = @contacttitle
UNION
SELECT * FROM TSQL2012.Production.Suppli ers
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.Suppli ers
WHERE country= @country
...
statement(s) where parameter value passed in IS NULL
and you execute the dynamic string by calling SP_EXECUTESQL
WHERE city = @city
UNION
SELECT * FROM TSQL2012.Production.Suppli
WHERE phone = @phone
UNION
SELECT * FROM TSQL2012.Production.Suppli
WHERE contacttitle = @contacttitle
UNION
SELECT * FROM TSQL2012.Production.Suppli
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.Suppli
WHERE country= @country
...
statement(s) where parameter value passed in IS NULL
and you execute the dynamic string by calling SP_EXECUTESQL
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ScottPletcher:
can you give me an example dynamic SQL
can you give me an example dynamic SQL
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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