﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Leon Platt / Article Discussions / Article Discussions by Author  / Making Dynamic Queries Static / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 04:52:32 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Making Dynamic Queries Static</title><link>http://www.sqlservercentral.com/Forums/Topic3839-28-1.aspx</link><description>&lt;P&gt;&lt;FONT color=#111111&gt;Hi Leon,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color=#111111&gt;Great article. I thnk even this solution would work:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color=#111111&gt;&lt;/FONT&gt; &lt;/P&gt;&lt;FONT size=2&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#111111&gt;&lt;STRONG&gt;&lt;FONT size=2&gt;IsNull&lt;/FONT&gt;&lt;FONT size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;state&lt;/FONT&gt;&lt;FONT size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt;''&lt;/FONT&gt;&lt;FONT size=2&gt;)&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT size=2&gt;like&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT size=2&gt;IsNull&lt;/FONT&gt;&lt;FONT size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;@State&lt;/FONT&gt;&lt;FONT size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt;''&lt;/FONT&gt;&lt;FONT size=2&gt;)&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT color=#111111&gt;&lt;STRONG&gt; &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#111111 size=2&gt;&lt;STRONG&gt;Or&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT color=#111111&gt;&lt;STRONG&gt; &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#111111&gt;&lt;STRONG&gt;&lt;FONT size=2&gt;state &lt;/FONT&gt;&lt;FONT size=2&gt;like&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT size=2&gt;IsNull&lt;/FONT&gt;&lt;FONT size=2&gt;(&lt;/FONT&gt;&lt;FONT size=2&gt;@state&lt;/FONT&gt;&lt;FONT size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt;state&lt;/FONT&gt;&lt;FONT size=2&gt;)+&lt;/FONT&gt;&lt;FONT size=2&gt;''&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color=#111111 size=2&gt;I am using this in a procedure and it seems to work fine.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color=#ff0000 size=2&gt;&lt;FONT color=#111111&gt;&lt;/FONT&gt; &lt;/P&gt;&lt;/FONT&gt;</description><pubDate>Mon, 26 Mar 2007 15:06:00 GMT</pubDate><dc:creator>Mohammad Fayaz</dc:creator></item><item><title>RE: Making Dynamic Queries Static</title><link>http://www.sqlservercentral.com/Forums/Topic3839-28-1.aspx</link><description>Sorry, I should use this instead of my last post:CREATE PROCEDURE sp_MySearch        @p1 varchar(50),        @p2 varchar(50)Asselect * from t where isnull(c1, isnull(@p1,'')) like  case when len(isnull(@p1,'')) =0 then isnull(c1, '') else '%'+@p1+'%' end andisnull(c2, isnull(@p2,'')) like  case when len(isnull(@p2,'')) =0 then isnull(c2, '') else '%'+@p2+'%' end </description><pubDate>Thu, 11 Jul 2002 14:37:00 GMT</pubDate><dc:creator>duanxn</dc:creator></item><item><title>RE: Making Dynamic Queries Static</title><link>http://www.sqlservercentral.com/Forums/Topic3839-28-1.aspx</link><description>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),ASselect * from t where isnull(c1, '') like  case when len(isnull(@p1,'')) =0 then isnull(c1, '') else '%'+@p1+'%' end andisnull(c2, '') like  case when len(isnull(@p2,'')) =0 then isnull(c2, '') else '%'+@p2+'%' endOf cause the performance using "like" is far poor than using a "=" </description><pubDate>Thu, 11 Jul 2002 14:23:00 GMT</pubDate><dc:creator>duanxn</dc:creator></item><item><title>RE: Making Dynamic Queries Static</title><link>http://www.sqlservercentral.com/Forums/Topic3839-28-1.aspx</link><description>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='')</description><pubDate>Mon, 06 May 2002 21:54:00 GMT</pubDate><dc:creator>Leon Platt</dc:creator></item><item><title>RE: Making Dynamic Queries Static</title><link>http://www.sqlservercentral.com/Forums/Topic3839-28-1.aspx</link><description>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 table1where (@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</description><pubDate>Thu, 02 May 2002 12:34:00 GMT</pubDate><dc:creator>toddbkc</dc:creator></item><item><title>RE: Making Dynamic Queries Static</title><link>http://www.sqlservercentral.com/Forums/Topic3839-28-1.aspx</link><description>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. </description><pubDate>Thu, 02 May 2002 10:20:00 GMT</pubDate><dc:creator>RyanRandall</dc:creator></item><item><title>RE: Making Dynamic Queries Static</title><link>http://www.sqlservercentral.com/Forums/Topic3839-28-1.aspx</link><description>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. </description><pubDate>Thu, 02 May 2002 07:20:00 GMT</pubDate><dc:creator>Lee Dise</dc:creator></item><item><title>RE: Making Dynamic Queries Static</title><link>http://www.sqlservercentral.com/Forums/Topic3839-28-1.aspx</link><description>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.Nameandcase 	when @Cfirst is null then C.firstName	else @Cfirst end = C.firstNameandcase 	when @Clast is null then C.lastName	else @Clast end = C.lastNameandcase 	when @city is null then D.city	else @cityend = D.cityandcase 	when @state is null then D.state	else @state end = D.stateandcase 	when @phone is null then  D.phone	else @phone end = D.phoneAndyandyj93@hotmail.com</description><pubDate>Thu, 02 May 2002 04:16:00 GMT</pubDate><dc:creator>andyj93</dc:creator></item><item><title>RE: Making Dynamic Queries Static</title><link>http://www.sqlservercentral.com/Forums/Topic3839-28-1.aspx</link><description>here's one way to solve the problem posed by soln. number twoand isnull(ColumnOne, '%') like @pSearchArgto use other operators (=, &amp;gt; etc.) try something likeand ColumnOne = insnull(@pSearchArg, ColumnOne)the optimiser can still use an index on ColumnOne if there is one </description><pubDate>Thu, 02 May 2002 03:47:00 GMT</pubDate><dc:creator>steven powell</dc:creator></item><item><title>RE: Making Dynamic Queries Static</title><link>http://www.sqlservercentral.com/Forums/Topic3839-28-1.aspx</link><description>One way to modify solution 2 to work with the mentioned operators (=, &amp;lt;&amp;gt;, &amp;gt; , &amp;lt;, 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</description><pubDate>Thu, 02 May 2002 01:53:00 GMT</pubDate><dc:creator>Robert W Marda</dc:creator></item><item><title>RE: Making Dynamic Queries Static</title><link>http://www.sqlservercentral.com/Forums/Topic3839-28-1.aspx</link><description>Thanks for the article, it is very useful.I would love to see your &amp;gt; 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 </description><pubDate>Wed, 01 May 2002 23:40:00 GMT</pubDate><dc:creator>jodiem</dc:creator></item><item><title>Making Dynamic Queries Static</title><link>http://www.sqlservercentral.com/Forums/Topic3839-28-1.aspx</link><description>Comments posted to this topic are about the content posted at &lt;A HREF=http://www.sqlservercentral.com/columnists/lplatt/makingdynamicqueriesstatic.asp&gt;http://www.sqlservercentral.com/columnists/lplatt/makingdynamicqueriesstatic.asp&lt;/A&gt;</description><pubDate>Sun, 28 Apr 2002 00:00:00 GMT</pubDate><dc:creator>Leon Platt</dc:creator></item></channel></rss>