<?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>Clarion Edge &#187; SQL</title>
	<atom:link href="http://www.clarionedge.com/category/clarion/sql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.clarionedge.com</link>
	<description>Get an Edge with Clarion!</description>
	<lastBuildDate>Thu, 29 Jul 2010 21:13:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Trailing spaces in strings &#8211; Clarion vs SQL</title>
		<link>http://www.clarionedge.com/clarion/trailing-spaces-in-strings-clarion-vs-sql.html</link>
		<comments>http://www.clarionedge.com/clarion/trailing-spaces-in-strings-clarion-vs-sql.html#comments</comments>
		<pubDate>Thu, 19 Feb 2009 16:54:22 +0000</pubDate>
		<dc:creator>brahn</dc:creator>
				<category><![CDATA[Clarion]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.clarionedge.com/?p=209</guid>
		<description><![CDATA[This topic came up at work a while ago when trying to perform database operations on strings containing a trailing space.
The Problem: One of our stored procedures was using CHARINDEX to search for strings within [...]]]></description>
			<content:encoded><![CDATA[<p>This topic came up at work a while ago when trying to perform database operations on strings containing a trailing space.</p>
<p>The Problem: One of our stored procedures was using CHARINDEX to search for strings within the database. The problem appeared when a user defined a search parameter containing a trailing space and the results of the search were not as expected.</p>
<h2>SQL and Trailing Blanks</h2>
<p>(From the section &#8220;Comparison operators, listed&#8221; in SQL help)</p>
<p>Trailing blanks are ignored in comparisons; for example, these are equivalent:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">WHERE</span> LastName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'White'</span>
<span style="color: #993333; font-weight: bold;">WHERE</span> LastName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'White '</span>
<span style="color: #993333; font-weight: bold;">WHERE</span> LastName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'White'</span> <span style="color: #66cc66;">+</span> SPACE<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is an interesting article: <a href="http://support.microsoft.com/kb/316626">INF: How SQL Server Compares Strings with Trailing Spaces</a></p>
<p>Sort of related but actually about how data is stored is the ANSI_PADDING setting:</p>
<p><a href="http://technet.microsoft.com/en-us/library/ms187403.aspx">http://technet.microsoft.com/en-us/library/ms187403.aspx</a></p>
<h2>Display Trailing Space in Clarion</h2>
<p>The Clarion entry control and text control don&#8217;t appear to be able to show trailing spaces from data in a database. You can enter trailing spaces in an entry or text control and save the data but next time you view or edit the form those trailing spaces are not shown.</p>
<p>Funnily enough, if the data <em>does </em>have trailing spaces and the field is not edited then the trailing spaces remain intact when the form is saved.</p>
<h2>Fun with SELECT Statements</h2>
<p>Have a play with the following SQL to see some examples:</p>
<p><em>Note the use of &#8220;LIKE&#8221; rather than &#8220;=&#8221; and the &#8220;[^ ]&#8221; will force the WHERE to only match items without the trailing space.<br />
</em><em>Also, try setting the ANSI_PADDING to &#8220;OFF&#8221; and see the difference in the results.</em></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SET</span> ANSI_PADDING <span style="color: #993333; font-weight: bold;">ON</span>;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> t1 <span style="color: #66cc66;">&#40;</span>charcol CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> varcharcol VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'No blanks'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'No blanks'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> t1 <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Trailing blank '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Trailing blank '</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t1 <span style="color: #993333; font-weight: bold;">WHERE</span> varcharcol <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'No blanks     '</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t1 <span style="color: #993333; font-weight: bold;">WHERE</span> charcol <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'No blanks     '</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t1 <span style="color: #993333; font-weight: bold;">WHERE</span> varcharcol <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'Trailing blank[^ ]'</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> t1 <span style="color: #993333; font-weight: bold;">WHERE</span> charcol <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'Trailing blank[^ ]'</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> t1</pre></div></div>

<p>So there are a few thoughts and discoveries that came up in the course of investigating this issue. Of course once you know the reasons there are ways to workaround the problems but I will leave that up to your individual scenario&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.clarionedge.com/clarion/trailing-spaces-in-strings-clarion-vs-sql.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Temp Tables, Scope and Clarion</title>
		<link>http://www.clarionedge.com/clarion/sql/temp-tables-scope-and-clarion.html</link>
		<comments>http://www.clarionedge.com/clarion/sql/temp-tables-scope-and-clarion.html#comments</comments>
		<pubDate>Tue, 11 Mar 2008 18:20:26 +0000</pubDate>
		<dc:creator>brahn</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.clarionedge.com/?p=17</guid>
		<description><![CDATA[I was discussing this with a colleague yesterday and thought everyone might be interested in the outcome.
Problem: 
I was getting an error &#8220;There is already an object named &#8216;PK_SUPPLIERPRODUCTID&#8217; in the database.&#8221; when trying to [...]]]></description>
			<content:encoded><![CDATA[<p>I was discussing this with a colleague yesterday and thought everyone might be interested in the outcome.</p>
<p><strong>Problem: </strong></p>
<p>I was getting an error &#8220;<em>There is already an object named &#8216;PK_SUPPLIERPRODUCTID&#8217; in the database.</em>&#8221; when trying to create a temp table.</p>
<p><strong>Scenario: </strong></p>
<ul>
<li>A temp table (&#8217;#Temp&#8217; &amp; Thread()) was created initially in a window procedure on thread 2.</li>
<li>That window was then closed and later opened on a different thread number e.g. 3.</li>
<li>When the CREATE TABLE is called the second time I was getting the above error.</li>
</ul>
<p><span id="more-17"></span></p>
<p><strong>Original SQL:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td 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;">TABLE</span> <span style="color: #808080; font-style: italic;">#Temp (SUPPLIERPRODUCTID int NOT NULL CONSTRAINT PK_SUPPLIERPRODUCTID PRIMARY KEY)</span></pre></td></tr></table></div>

<p><strong>Gotcha&#8217;s:</strong></p>
<ul>
<li>Normally the details of a temp table are &#8220;session&#8221; dependent. It seems that because I was explicitly naming the primary key (PK_SUPPLIERPRODUCTID) this was somehow bypassing the way temp tables are assigned session unique names.</li>
<li>Scope of SQL temp tables:
<ul>
<li>&#8220;All other local temporary tables are dropped automatically at the end of the  current session.&#8221;</li>
</ul>
<ul>
<li>I had always thought that a &#8220;session&#8221; would mean the Clarion Thread connection.
<ul>
<li>It kinda does and kinda doesn&#8217;t.</li>
<li>Temp tables are assigned uniqueness based on the clarion thread connection <strong><em>but </em></strong>they are not removed from the database when the clarion thread closes.</li>
<li>They are only actually dropped from the SQL tempdb when the clarion application closes!</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><strong>Solutions:</strong></p>
<ul>
<li>Don&#8217;t explicitly name the primary key! This works without error:

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td 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;">TABLE</span> <span style="color: #808080; font-style: italic;">#Temp (SUPPLIERPRODUCTID int PRIMARY KEY)</span></pre></td></tr></table></div>

</li>
<li>If you do a &#8220;CREATE TABLE&#8221; then try and make sure to do a &#8220;DROP TABLE&#8221; once you are done (perhaps as well as before the create?).
<ul>
<li>This has the added benefit of keeping the tempdb cleaner.</li>
</ul>
<ul>
<li>I guess this is just basic house keeping like in clarion when you must always New/Dispose.</li>
</ul>
</li>
</ul>
<p><span class="info"> Disclaimer: I don&#8217;t claim to be an SQL expert, these are my observations and my solutions. I would love to hear any comments!</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.clarionedge.com/clarion/sql/temp-tables-scope-and-clarion.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
