Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Making Dynamic Queries Static Expand / Collapse
Author
Message
Posted Sunday, April 28, 2002 12:00 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Saturday, February 10, 2007 12:11 PM
Points: 91, Visits: 1
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/lplatt/makingdynamicqueriesstatic.asp


Post #3839
Posted Wednesday, May 1, 2002 11:40 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, February 19, 2003 12:00 AM
Points: 14, Visits: 1
Thanks for the article, it is very useful.
I would love to see your > solution and can I ask for a between solution also.
This is the query that I'm trying to do at the moment:

I need a query that does both And's or Ors and works on multiple tables. Eg the query is about house features The Square foot is stored in one table The rooms are stored in another table The features are stored in another table again (Similar to your scenario in your article).

The user wants to query a house that is between 2000 and 3000 square foot, with 3 bathrooms (from the rooms table) and with the following features eg porch, fireplace (each stored as a record in the features table)

That was the simplified version, I would love to be able to expand it to query other things that we store about the houses.

Jodie





Post #32592
Posted Thursday, May 2, 2002 1:53 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, November 5, 2013 9:05 AM
Points: 976, Visits: 59
One way to modify solution 2 to work with the mentioned operators (=, <>, > , <, IN) is to create the WHERE clause like this:

WHERE (A.name = @Aname or @Aname = '') and (C.firstName like @Cfirst or @Cfirst = '') and (C.lastName like @Clast or @Clast = '') and (D.city like @city or @city = '') and (D.state = @state or D.state = '') and (D.phone like @phone or @phone = '')

This solution removes the assumption that a NULL record is included. It also means that if you do not want to limit the result set by a city name simply send in @city = '' and that part of the WHERE clause will be completely ignored. It will just limit the result set on all other parameters that are not equal to ''.

Robert Marda




Robert W. Marda
SQL Programmer
Ipreo
Post #32593
Posted Thursday, May 2, 2002 3:47 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, May 12, 2009 9:33 AM
Points: 157, Visits: 2
here's one way to solve the problem posed by soln. number two

and isnull(ColumnOne, '%') like @pSearchArg

to use other operators (=, > etc.) try something like

and ColumnOne = insnull(@pSearchArg, ColumnOne)

the optimiser can still use an index on ColumnOne if there is one




Post #32594
Posted Thursday, May 2, 2002 4:16 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 3:45 AM
Points: 1,130, Visits: 370
Hi, how about :-

SELECT
*
from
account A
inner Join Contact C on a.primarykey=c.accountkey
inner join Address D on A.primarykey=D.foreignkey
WHERE
case
when @Aname is null then A.Name
else @Aname
end = A.Name
and
case
when @Cfirst is null then C.firstName
else @Cfirst
end = C.firstName
and
case
when @Clast is null then C.lastName
else @Clast
end = C.lastName
and
case
when @city is null then D.city
else @city
end = D.city
and
case
when @state is null then D.state
else @state
end = D.state
and
case
when @phone is null then D.phone
else @phone
end = D.phone

Andy
andyj93@hotmail.com



Regards,
Andy Jones
Post #32595
Posted Thursday, May 2, 2002 7:20 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, November 5, 2014 7:18 AM
Points: 284, Visits: 19
The only time you absolutely need to use dynamic SQL is when, at compile time, you do not know the names of the database or the database objects against which your query will execute.

WHERE criteria do not count: As the author pointed out, there are ways to construct a WHERE clause to go this way or that depending upon whether a parameter value is missing.

It's the FROM clause that makes dynamic SQL essential. If, for example, I wish to write a procedure that performs a query against system tables, and I want at run time to specify which database I'm querying. The only way to turn the database name into a variable is through dynamic SQL. Or if your database contains two or more tables having the same or similar structure, and again you won't know until run-time which tables are to be queried... this is a job for dynamic SQL.





Post #32596
Posted Thursday, May 2, 2002 10:20 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, May 29, 2012 11:22 AM
Points: 1,755, Visits: 4,652
I guess you could change your FROM situation to a WHERE situation by creating a view of the union of your similar tables (together with a flag to indicate where they're from).

This would, of course, have other implications.




Ryan Randall

Solutions are easy. Understanding the problem, now, that's the hard part.
Post #32597
Posted Thursday, May 2, 2002 12:34 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, June 17, 2014 2:54 PM
Points: 27, Visits: 256
I have had good luck using the OR statement instead of LIKE. Often, I find that the LIKE statement doesn't always like to read the index.

So, when I do this, the statement looks like:
select * from table1
where (@first_name IS NULL or first_name = @first_name)
and (@last_name IS NULL or last_name = @last_name) etc...

I've found really great performance overall in the majority of cases. Of course, this doesn't handle if you really wanted the LIKE statement, but often I am looking for an exact match.

Todd



Todd
Post #32598
Posted Monday, May 6, 2002 9:54 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Saturday, February 10, 2007 12:11 PM
Points: 91, Visits: 1
Thanks; everyone for the responses to my article. I will try to address everyone in the next day or two. Lets start with spowell_2 because your solution is basically the same as mine was. My solution was:
isnull(Column, ' ') like @Parameter.
Unfortunately you take a little hit with cpu time to do the isnull function on each column but its a very clean solution.

tbredehoft - what do you do with the parameters where the user does not enter any search critera? You end up with a situation like where (firstName is null or firstname='')




Post #32599
Posted Thursday, July 11, 2002 2:23 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 16, 2005 10:13 AM
Points: 4, Visits: 1
Hi, I think

ColumnOne = insnull(@pSearchArg, ColumnOne)

whill not work, because null value in ColumnOne will fail this expression.
I'd like to use something like the following if you insist on using "like":

CREATE PROCEDURE sp_MySearch
@p1 varchar(50),
@p2 varchar(50),

AS
select * from t where
isnull(c1, '') like case when len(isnull(@p1,'')) =0 then isnull(c1, '') else '%'+@p1+'%' end and
isnull(c2, '') like case when len(isnull(@p2,'')) =0 then isnull(c2, '') else '%'+@p2+'%' end


Of cause the performance using "like" is far poor than using a "="






Post #32600
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse