<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://2pttechnology.com/web/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SQL Scripts</title><link>http://2pttechnology.com/web/forums/15/ShowForum.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.2)</generator><item><title>SQL Exists Condition / Not Exists Condition</title><link>http://2pttechnology.com/web/forums/thread/183.aspx</link><pubDate>Thu, 05 Jun 2008 23:08:54 GMT</pubDate><guid isPermaLink="false">a285717b-1f26-4449-a706-ef28376a7378:183</guid><dc:creator>admin</dc:creator><slash:comments>0</slash:comments><comments>http://2pttechnology.com/web/forums/thread/183.aspx</comments><wfw:commentRss>http://2pttechnology.com/web/forums/commentrss.aspx?SectionID=15&amp;PostID=183</wfw:commentRss><description>&lt;P&gt;The EXISTS condition is considered "to be met" if the subquery returns at least one row.&lt;/P&gt;
&lt;P&gt;The syntax for the EXISTS condition is:&lt;/P&gt;
&lt;BLOCKQUOTE class=definition&gt;
&lt;P&gt;SELECT columns&lt;BR&gt;FROM tables&lt;BR&gt;WHERE EXISTS ( subquery );&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The EXISTS condition can be used in any valid SQL statement - select, insert, update, or delete.&lt;/P&gt;&lt;BR&gt;
&lt;P class=example_heading&gt;Example #1&lt;/P&gt;
&lt;P&gt;Let's take a look at a simple example. The following is an SQL statement that uses the EXISTS condition:&lt;/P&gt;
&lt;BLOCKQUOTE class=sql_command&gt;
&lt;P&gt;SELECT *&lt;BR&gt;FROM suppliers&lt;BR&gt;WHERE EXISTS&lt;BR&gt;&amp;nbsp; (select *&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; from orders&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; where suppliers.supplier_id = orders.supplier_id);&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This select statement will return all records from the suppliers table where there is at least one record in the orders table with the same supplier_id.&lt;BR&gt;&lt;/P&gt;
&lt;P class=example_heading&gt;Example #2 - NOT EXISTS&lt;/P&gt;
&lt;P&gt;The EXISTS condition can also be combined with the NOT operator.&lt;/P&gt;
&lt;P&gt;For example,&lt;/P&gt;
&lt;BLOCKQUOTE class=sql_command&gt;
&lt;P&gt;SELECT *&lt;BR&gt;FROM suppliers&lt;BR&gt;WHERE not exists (select * from orders Where suppliers.supplier_id = orders.supplier_id);&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This will return all records from the suppliers table where there are &lt;B&gt;no&lt;/B&gt; records in the &lt;I&gt;orders&lt;/I&gt; table for the given supplier_id.&lt;/P&gt;&lt;BR&gt;
&lt;P class=example_heading&gt;Example #3 - DELETE Statement&lt;/P&gt;
&lt;P&gt;The following is an example of a delete statement that utilizes the EXISTS condition:&lt;/P&gt;
&lt;BLOCKQUOTE class=sql_command&gt;
&lt;P&gt;DELETE FROM suppliers&lt;BR&gt;WHERE EXISTS&lt;BR&gt;&amp;nbsp; (select *&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; from orders&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; where suppliers.supplier_id = orders.supplier_id);&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;BR&gt;
&lt;P class=example_heading&gt;Example #4 - UPDATE Statement&lt;/P&gt;
&lt;P&gt;The following is an example of an update statement that utilizes the EXISTS condition:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;TABLE class=sql_command cellSpacing=0 cellPadding=0&gt;

&lt;TR class=tr_left_top&gt;
&lt;TD&gt;UPDATE suppliers&lt;/TD&gt;
&lt;TD&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR class=tr_left_top&gt;
&lt;TD&gt;SET supplier_name =&lt;/TD&gt;
&lt;TD&gt;( SELECT customers.name&lt;BR&gt;FROM customers&lt;BR&gt;WHERE customers.customer_id = suppliers.supplier_id)&lt;/TD&gt;&lt;/TR&gt;
&lt;TR class=tr_left_top&gt;
&lt;TD colSpan=2&gt;WHERE EXISTS&lt;BR&gt;&amp;nbsp; ( SELECT customers.name&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM customers&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE customers.customer_id = suppliers.supplier_id);&lt;/TD&gt;&lt;/TR&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR&gt;
&lt;P class=example_heading&gt;Example #5 - INSERT Statement&lt;/P&gt;
&lt;P&gt;The following is an example of an insert statement that utilizes the EXISTS condition:&lt;/P&gt;
&lt;BLOCKQUOTE class=sql_command&gt;
&lt;P&gt;INSERT INTO suppliers&lt;BR&gt;(supplier_id, supplier_name)&lt;BR&gt;SELECT account_no, name&lt;BR&gt;FROM suppliers&lt;BR&gt;WHERE exists (select * from orders Where suppliers.supplier_id = orders.supplier_id);&lt;/P&gt;&lt;/BLOCKQUOTE&gt;</description></item></channel></rss>