<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Smart Pandian &#187; sql</title> <atom:link href="http://smartpandian.com/tag/sql/feed/" rel="self" type="application/rss+xml" /><link>http://smartpandian.com</link> <description>as simple as that...</description> <lastBuildDate>Sun, 27 Nov 2011 05:17:18 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>Where 1=1</title><link>http://smartpandian.com/where-11/</link> <comments>http://smartpandian.com/where-11/#comments</comments> <pubDate>Fri, 08 May 2009 05:41:58 +0000</pubDate> <dc:creator>Pandian</dc:creator> <category><![CDATA[lazy programmer]]></category> <category><![CDATA[sql]]></category> <category><![CDATA[where 1=1]]></category> <category><![CDATA[where 1=2]]></category> <guid
isPermaLink="false">http://smartpandian.com/?p=292</guid> <description><![CDATA[There were better ways to do the same
No related posts.]]></description> <content:encoded><![CDATA[<p>Its everywhere 1 is equal to 1, unless one does a incorrect math.  You may wonder &#8220;why would one write an extra condition in a SQL which doesn&#8217;t make any difference, and that adds burden to oneself and to others with wierd extra condition?&#8221;</p><p>Consider the following SQL,</p><pre class="brush: sql; title: ; notranslate">select * from dept where 1=1 and id = 5000;</pre><p>This sql query is, of course same as</p><pre class="brush: sql; title: ; notranslate">select * from dept where id = 5000;</pre><p>Basically, this condition &#8220;1=1&#8243; is used by lazy programmers, who wants to create an sql on the fly,i.e, dynamically through scripting, For example, Consider the below vb script.</p><pre class="brush: vb; title: ; notranslate">
strSqlStatement =  &quot;Select * from Debt where 1=1&quot;
For Each Obj in Conditions
strSqlStatement = strSqlStatement &amp; &quot; and &quot; &amp; Obj
Next
</pre><p>This is a lazy approach, as programmer could have identified the first condition and prefixed it  with &#8220;where&#8221; , instead of adding &#8220;and&#8221; to every conditions, i.e. as following script.</p><pre class="brush: vb; title: ; notranslate">
strSqlStatement =  &quot;Select * from Debt &quot;
i = 0
For Each Obj in Conditions
If i  = 0  then
strSqlStatement = strSqlStatement &amp; &quot; where &quot; &amp; Obj
Else
strSqlStatement = strSqlStatement &amp; &quot; and &quot; &amp; Obj
End If
i = i + 1
Next
</pre><p>Similar to &#8220;where 1=1&#8243;, there is another lazy programmer approach, its &#8220;where 1=2&#8243;. The purpose of &#8220;where 1=2&#8243; is to extract the list of column names in a table, and dynamically create &#8220;CREATE &#8221; or &#8220;INSERT &#8221; sql statements without fetching a record. Similar to the previous, There were better ways to do the same.</p><p>Ref:<br
/> * <a
href="http://www.novicksoftware.com/TipsAndTricks/tip-sql-server-where-1-equals-2.htm"><em>http://www.novicksoftware.com/TipsAndTricks/tip-sql-server-where-1-equals-2.htm</em></a><br
/> * <a
href="http://stackoverflow.com/questions/242822/why-would-someone-use-where-11-and-conditions-in-a-sql-clause"><cite>http://stackoverflow.com/questions/242822/why-would-someone-use-<strong>where-11</strong>-and-conditions-in-a-<strong>sql</strong>-clause</cite></a></p><p>No related posts.</p>]]></content:encoded> <wfw:commentRss>http://smartpandian.com/where-11/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
