<?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>MacLochlainns Weblog &#187; DBMS_SQL</title>
	<atom:link href="http://blog.mclaughlinsoftware.com/tag/dbms_sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mclaughlinsoftware.com</link>
	<description>Michael McLaughlin's Technical Blog</description>
	<lastBuildDate>Mon, 06 Sep 2010 21:21:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Entering a colon in an NDS statement</title>
		<link>http://blog.mclaughlinsoftware.com/2008/05/10/entering-a-colon-in-an-nds-statement/</link>
		<comments>http://blog.mclaughlinsoftware.com/2008/05/10/entering-a-colon-in-an-nds-statement/#comments</comments>
		<pubDate>Sat, 10 May 2008 04:58:24 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[CHR(58)]]></category>
		<category><![CDATA[colon]]></category>
		<category><![CDATA[DBMS_SQL]]></category>
		<category><![CDATA[nds]]></category>
		<category><![CDATA[ORA-00947]]></category>
		<category><![CDATA[ORA-01006]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[pl/sql]]></category>

		<guid isPermaLink="false">http://maclochlainn.wordpress.com/?p=12</guid>
		<description><![CDATA[Somebody asked me how you include a colon in a Native Dynamic SQL (NDS) statement when it’s not related to a placeholder. A colon without a placeholder raises an ORA-00947 error when parsing the statement. The error means you’ve failed to submit enough bind variables. You can substitute a CHR(58) where you need to insert [...]]]></description>
			<content:encoded><![CDATA[<p>Somebody asked me how you include a <em>colon</em> in a Native Dynamic SQL (NDS) statement when it’s not related to a placeholder. A colon without a placeholder raises an <code>ORA-00947</code> error when parsing the statement. The error means you’ve failed to submit enough bind variables. You can substitute a <tt>CHR(58)</tt> where you need to insert the standalone colon. The NDS or <code>DBMS_SQL</code> parsing phase ignores a <tt>CHR(58)</tt>, which translates during actual SQL statement parsing as a colon.</p>
<p>Let’s say you want to insert a column value with an ASIN (Amazon Standard Identification Number) code in the format: <code>ASIN: B000VBJEEG</code></p>
<p>Using NDS, you have two choices. You can let the entry person type the full string and pass that string as a bind variable, or you can substitute <tt>CHR(58)</tt> for the colon and enter only the ASIN code. The example (<a title="Oracle Database 11g PL/SQL Programming" href="http://www.amazon.com/Oracle-Database-11g-Programming-Press/dp/0071494456/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1210391233&amp;sr=8-1" target="_blank">Oracle Database 11<em>g</em> PL/SQL Programming</a>, pp. 386-387) implements the latter:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> PROCEDURE insert_item
<span style="color: #66cc66;">&#40;</span> asin VARCHAR2
<span style="color: #66cc66;">,</span> item_type VARCHAR2
<span style="color: #66cc66;">,</span> item_title VARCHAR2
<span style="color: #66cc66;">,</span> item_subtitle VARCHAR2 :<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">''</span>
<span style="color: #66cc66;">,</span> rating VARCHAR2
<span style="color: #66cc66;">,</span> agency VARCHAR2
<span style="color: #66cc66;">,</span> release_date DATE <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">IS</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Local variable for a dynamic SQL statement.</span>
  stmt VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2000</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
BEGIN
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Create a dynamic statement with bind variables.</span>
  stmt :<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'INSERT INTO item '</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">'( item_id'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', item_barcode'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', item_type'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', item_title'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', item_subtitle'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', item_desc'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', item_blob'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', item_photo'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', item_rating'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', item_rating_agency'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', item_release_date'</span> 
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', created_by'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', creation_date'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', last_updated_by'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', last_update_date ) '</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">'VALUES '</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">'( item_s1.nextval'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">','</span><span style="color: #ff0000;">'ASIN'</span><span style="color: #ff0000;">'||CHR(58)||:asin'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">',(SELECT common_lookup_id'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' FROM common_lookup'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">' WHERE common_lookup_type = :item_type)'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', :item_title'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', :item_subtitle'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', empty_clob()'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', NULL, NULL'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', :rating'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', :agency'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', :release_date'</span>
  <span style="color: #66cc66;">||</span> <span style="color: #ff0000;">', 3, SYSDATE, 3, SYSDATE)'</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Print debug statement.</span>
  dbms_output<span style="color: #66cc66;">.</span>put_line<span style="color: #66cc66;">&#40;</span>stmt<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">-- Execute dynamic statement with bind variables.</span>
  EXECUTE IMMEDIATE stmt
  <span style="color: #993333; font-weight: bold;">USING</span> asin<span style="color: #66cc66;">,</span> item_type<span style="color: #66cc66;">,</span> item_title<span style="color: #66cc66;">,</span> item_subtitle<span style="color: #66cc66;">,</span> rating<span style="color: #66cc66;">,</span> agency<span style="color: #66cc66;">,</span> release_date;
END insert_item;
<span style="color: #66cc66;">/</span></pre></div></div>

<p>There&#8217;s quite a nifty or nasty trick inside the dynamic SQL statement. You&#8217;ll notice that the colon is concatenated to the <code>ASIN</code> and a bind variable. It is critical that you don&#8217;t encapsulate the bind variable inside quotes, or you&#8217;ll raise an <code>ORA-01006</code> exception (check Table 11-2 in the Oracle Database 11g PL/SQL Programming book for more detail). You can&#8217;t enclose a string in single quotes because the string is substituted as a string, and it trips up the parser.</p>
<p>You can test this dynamic SQL statement with the following anonymous PL/SQL block:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">BEGIN
  insert_item<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'B00005JPO1'</span>
             <span style="color: #66cc66;">,</span><span style="color: #ff0000;">'DVD_WIDE_SCREEN'</span>
             <span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Indiana Jones and the Crystal Skull'</span>
             <span style="color: #66cc66;">,</span><span style="color: #ff0000;">'2-Disc Special Edition'</span>
             <span style="color: #66cc66;">,</span><span style="color: #ff0000;">'PG-13'</span>
             <span style="color: #66cc66;">,</span><span style="color: #ff0000;">'MPAA'</span>
             <span style="color: #66cc66;">,</span><span style="color: #ff0000;">'14-OCT-08'</span><span style="color: #66cc66;">&#41;</span>;
END;
<span style="color: #66cc66;">/</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2008/05/10/entering-a-colon-in-an-nds-statement/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
