<?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"
	>

<channel>
	<title>Fransiscus Setiawan .NET Blog &#187; SQL Server</title>
	<atom:link href="http://fransiscuss.com/category/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://fransiscuss.com</link>
	<description>My notepad to share knowledge with others</description>
	<pubDate>Wed, 06 Jan 2010 05:44:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>Shrink all databases and Set it to Simple in SQL Server</title>
		<link>http://fransiscuss.com/shrink-all-databases-and-set-it-to-simple-in-sql-server/</link>
		<comments>http://fransiscuss.com/shrink-all-databases-and-set-it-to-simple-in-sql-server/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 05:02:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=249</guid>
		<description><![CDATA[Normally, I need to run this script against dev sql server in order for me to free up some space

CREATE TABLE #temp_dbs_table
&#40;
&#91;db_name&#93; sysname NOT NULL PRIMARY KEY,
&#91;mod&#93; tinyint NOT NULL DEFAULT 1
&#41;
&#160;
INSERT INTO #temp_dbs_table ([db_name])
SELECT
name
FROM
master..sysdatabases
WHERE
dbid &#38;gt; 4 --- skip master, tempdb, model and msdb databases
&#160;
declare @db_name sysname
&#160;
SET @db_name = ''
&#160;
while @db_name IS NOT NULL
begin
SET @db_name [...]]]></description>
			<content:encoded><![CDATA[<p>Normally, I need to run this script against dev sql server in order for me to free up some space</p>

<div class="wp_syntax"><div class="code"><pre class="sql 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_dbs_table</span>
<span style="color: #66cc66;">&#40;</span>
<span style="color: #66cc66;">&#91;</span>db_name<span style="color: #66cc66;">&#93;</span> sysname <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span>
<span style="color: #66cc66;">&#91;</span>mod<span style="color: #66cc66;">&#93;</span> tinyint <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #cc66cc;">1</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #808080; font-style: italic;">#temp_dbs_table ([db_name])</span>
<span style="color: #993333; font-weight: bold;">SELECT</span>
name
<span style="color: #993333; font-weight: bold;">FROM</span>
master<span style="color: #66cc66;">..</span>sysdatabases
<span style="color: #993333; font-weight: bold;">WHERE</span>
dbid &amp;gt; <span style="color: #cc66cc;">4</span> <span style="color: #808080; font-style: italic;">--- skip master, tempdb, model and msdb databases</span>
&nbsp;
declare @db_name sysname
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @db_name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">''</span>
&nbsp;
while @db_name <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
begin
<span style="color: #993333; font-weight: bold;">SET</span> @db_name <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">NULL</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> top <span style="color: #cc66cc;">1</span> @db_name <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>db_name<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #808080; font-style: italic;">#temp_dbs_table where [mod] = 1</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">IF</span> @db_name <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span>
break
&nbsp;
print <span style="color: #ff0000;">'--------------------------------------------------'</span>
&nbsp;
print <span style="color: #ff0000;">'&amp;gt; Database: '</span> <span style="color: #66cc66;">+</span> @db_name
&nbsp;
print <span style="color: #ff0000;">'&amp;gt; Changing recovery mode to simple'</span>
&nbsp;
declare @n_cmd nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @n_cmd <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'alter database ['</span> <span style="color: #66cc66;">+</span> @db_name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'] set recovery simple'</span>
&nbsp;
exec sp_executesql @n_cmd
&nbsp;
print <span style="color: #ff0000;">'&amp;gt; Shrinking database'</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @n_cmd <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'dbcc shrinkdatabase(['</span> <span style="color: #66cc66;">+</span> @db_name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'])'</span>
&nbsp;
exec sp_executesql @n_cmd
&nbsp;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #808080; font-style: italic;">#temp_dbs_table set [mod] = 0 where [db_name] = @db_name</span>
end
&nbsp;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #808080; font-style: italic;">#temp_dbs_table</span></pre></div></div>

<p>I got this script from <a href="http://www.sqlservercentral.com/scripts/Backup+%2F+Restore/61952/" target="_blank">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/shrink-all-databases-and-set-it-to-simple-in-sql-server/feed/</wfw:commentRss>
		</item>
		<item>
		<title>TRY&#8230;.CATCH Rollback Transaction In SQL Server 2005</title>
		<link>http://fransiscuss.com/trycatch-rollback-transaction-in-sql-server-2005/</link>
		<comments>http://fransiscuss.com/trycatch-rollback-transaction-in-sql-server-2005/#comments</comments>
		<pubDate>Sat, 23 May 2009 01:16:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[ROLLBACK]]></category>

		<category><![CDATA[TRANSACTION]]></category>

		<category><![CDATA[TRY CATCH]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=215</guid>
		<description><![CDATA[This feature has been sometime in SQL Server 2005 in SQL Server 2000 you normally use @@TRANCOUNT to detect any exception but now in SQL Server 2005 you can use try catch block.
In this snippet, there are 2 INSERT statement and 1 UPDATE statement. I&#8217;m expecting when there is any failure (e.g the UPDATE statement [...]]]></description>
			<content:encoded><![CDATA[<p>This feature has been sometime in SQL Server 2005 in SQL Server 2000 you normally use @@TRANCOUNT to detect any exception but now in SQL Server 2005 you can use try catch block.</p>
<p>In this snippet, there are 2 INSERT statement and 1 UPDATE statement. I&#8217;m expecting when there is any failure (e.g the UPDATE statement fails) then all the preceding INSERT/UPDATE/DELETE within the TRANSACTION block will be canceled</p>
<p>e.g</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;">BEGIN TRY
BEGIN TRANSACTION transABC
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> TABLEA <span style="color: #66cc66;">&#40;</span>col1<span style="color: #66cc66;">,</span>col2<span style="color: #66cc66;">,</span>col3<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'b'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'c'</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> TABLEB <span style="color: #66cc66;">&#40;</span>col1<span style="color: #66cc66;">,</span>col2<span style="color: #66cc66;">,</span>col3<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'b'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'c'</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">UPDATE</span> TABLEA <span style="color: #993333; font-weight: bold;">SET</span> col2<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'abcde'</span> <span style="color: #993333; font-weight: bold;">WHERE</span> col1 <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'a'</span>
&nbsp;
COMMIT TRANSACTION transABC
END TRY
&nbsp;
BEGIN CATCH
<span style="color: #993333; font-weight: bold;">IF</span> @@TRANCOUNT <span style="color: #66cc66;">&gt;</span> 0
ROLLBACK TRANSACTION transABC <span style="color: #808080; font-style: italic;">--RollBack in case of Error</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Raise an error with the details of the exception</span>
DECLARE @ErrMsg nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @ErrSeverity int
<span style="color: #993333; font-weight: bold;">SELECT</span> @ErrMsg <span style="color: #66cc66;">=</span> ERROR_MESSAGE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
@ErrSeverity <span style="color: #66cc66;">=</span> ERROR_SEVERITY<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
RAISERROR<span style="color: #66cc66;">&#40;</span>@ErrMsg<span style="color: #66cc66;">,</span> @ErrSeverity<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
END CATCH</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/trycatch-rollback-transaction-in-sql-server-2005/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Index in Table Variable</title>
		<link>http://fransiscuss.com/index-in-table-variable/</link>
		<comments>http://fransiscuss.com/index-in-table-variable/#comments</comments>
		<pubDate>Thu, 14 May 2009 07:32:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=212</guid>
		<description><![CDATA[One biggest advantage of table variable for me is you can&#8217;t put index on the columns. I was having a problem where doing self join in table variable (it has 1000+ records) is taking really really long time. One way I found to optimize it is to add primary key assuming every single record is [...]]]></description>
			<content:encoded><![CDATA[<p>One biggest advantage of table variable for me is you can&#8217;t put index on the columns. I was having a problem where doing self join in table variable (it has 1000+ records) is taking really really long time. One way I found to optimize it is to add <strong>primary key</strong> assuming every single record is unique on the table variable. The performance improvement is really significant by adding the primary key on table variable. And <strong>by changing table variable to temp table</strong>, it improves the performance more than by twice</p>

<div class="wp_syntax"><div class="code"><pre class="sql 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;">#dtblJobsBillDates</span>
<span style="color: #66cc66;">&#40;</span>
		chProcessID			CHAR<span style="color: #66cc66;">&#40;</span>03<span style="color: #66cc66;">&#41;</span>		COLLATE database_default <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
		intAssgnmtID		INTEGER									 <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
		chBillOfficeCode	CHAR<span style="color: #66cc66;">&#40;</span>03<span style="color: #66cc66;">&#41;</span>		COLLATE database_default <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
		chBillEntityCode	CHAR<span style="color: #66cc66;">&#40;</span>03<span style="color: #66cc66;">&#41;</span>		COLLATE database_default <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
		chBillNo			CHAR<span style="color: #66cc66;">&#40;</span>08<span style="color: #66cc66;">&#41;</span>		COLLATE database_default <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
		<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>intAssgnmtID<span style="color: #66cc66;">,</span> chBillNo<span style="color: #66cc66;">,</span> chProcessID<span style="color: #66cc66;">,</span> chBillOfficeCode<span style="color: #66cc66;">,</span> chBillEntityCode<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/index-in-table-variable/feed/</wfw:commentRss>
		</item>
		<item>
		<title>SQL Server Function using CLR</title>
		<link>http://fransiscuss.com/sql-server-function-using-clr/</link>
		<comments>http://fransiscuss.com/sql-server-function-using-clr/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 23:47:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=196</guid>
		<description><![CDATA[UPDATED: I&#8217;ve added one function to write from BLOB in SQL Server table to the disk straight away
I thought this article might be useful for anyone that wants to implement .NET code to SQL server level. In this case I really need CLR because I want to do compression of images and I believe it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATED: I&#8217;ve added one function to write from BLOB in SQL Server table to the disk straight away</strong></p>
<p>I thought this article might be useful for anyone that wants to implement .NET code to SQL server level. In this case I really need CLR because I want to do compression of images and I believe it&#8217;s not possible to do that using pure SQL server stored procedure and I&#8217;m trying to avoid creating a .NET application just for compression of images through row by row processing. </p>
<p>Here we start:</p>
<p>This is your C#/.NET code, you need to declare it as SQL function or SQL stored procedure</p>

<div class="wp_syntax"><div class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data.SqlTypes</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Server</span>;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Zip </span><span style="color: #008000;">=</span> ICSharpCode.<span style="color: #0000FF;">SharpZipLib</span>.<span style="color: #0000FF;">Zip</span>.<span style="color: #0000FF;">Compression</span>;
&nbsp;
<span style="color: #0600FF;">namespace</span> CLRCompressionFunctions
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> partial <span style="color: #FF0000;">class</span> CompressionCore
    <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#91;</span>SqlFunction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> SqlBytes fn_Compress<span style="color: #000000;">&#40;</span>SqlBytes uncompressedBytes<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>uncompressedBytes.<span style="color: #0000FF;">IsNull</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> uncompressedBytes;
&nbsp;
            MemoryStream memory <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            ICSharpCode.<span style="color: #0000FF;">SharpZipLib</span>.<span style="color: #0000FF;">Zip</span>.<span style="color: #0000FF;">Compression</span>.<span style="color: #0000FF;">Streams</span>.<span style="color: #0000FF;">DeflaterOutputStream</span> stream <span style="color: #008000;">=</span>
                <span style="color: #008000;">new</span> ICSharpCode.<span style="color: #0000FF;">SharpZipLib</span>.<span style="color: #0000FF;">Zip</span>.<span style="color: #0000FF;">Compression</span>.<span style="color: #0000FF;">Streams</span>.<span style="color: #0000FF;">DeflaterOutputStream</span><span style="color: #000000;">&#40;</span>memory, <span style="color: #008000;">new</span> Zip.<span style="color: #0000FF;">Deflater</span><span style="color: #000000;">&#40;</span>Zip.<span style="color: #0000FF;">Deflater</span>.<span style="color: #0000FF;">BEST_COMPRESSION</span><span style="color: #000000;">&#41;</span>, <span style="color: #FF0000;">131072</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            stream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>uncompressedBytes.<span style="color: #0000FF;">Buffer</span>, 0, Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>uncompressedBytes.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
            stream.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            stream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            stream <span style="color: #008000;">=</span> null;
&nbsp;
            <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> SqlBytes<span style="color: #000000;">&#40;</span>memory.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#91;</span>SqlFunction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> SqlBytes fn_Decompress<span style="color: #000000;">&#40;</span>SqlBytes compressedBytes<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>compressedBytes.<span style="color: #0000FF;">IsNull</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0600FF;">return</span> compressedBytes;
&nbsp;
            ICSharpCode.<span style="color: #0000FF;">SharpZipLib</span>.<span style="color: #0000FF;">Zip</span>.<span style="color: #0000FF;">Compression</span>.<span style="color: #0000FF;">Streams</span>.<span style="color: #0000FF;">InflaterInputStream</span> stream <span style="color: #008000;">=</span>
                <span style="color: #008000;">new</span> ICSharpCode.<span style="color: #0000FF;">SharpZipLib</span>.<span style="color: #0000FF;">Zip</span>.<span style="color: #0000FF;">Compression</span>.<span style="color: #0000FF;">Streams</span>.<span style="color: #0000FF;">InflaterInputStream</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> MemoryStream<span style="color: #000000;">&#40;</span>compressedBytes.<span style="color: #0000FF;">Buffer</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
            MemoryStream memory <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MemoryStream<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> writeData <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">4096</span><span style="color: #000000;">&#93;</span>;
            <span style="color: #FF0000;">int</span> size;
&nbsp;
            <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                size <span style="color: #008000;">=</span> stream.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>writeData, 0, writeData.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span>;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>size <span style="color: #008000;">&amp;</span>gt; 0<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    memory.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>writeData, 0, size<span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">else</span> break;
            <span style="color: #000000;">&#125;</span>
            stream.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            stream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            stream <span style="color: #008000;">=</span> null;
&nbsp;
            <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> SqlBytes<span style="color: #000000;">&#40;</span>memory.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>SqlFunction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> SqlString fn_WriteFile<span style="color: #000000;">&#40;</span>SqlString path, SqlBytes bytesFile, SqlBoolean isCompressed<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> returnString <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span>;
&nbsp;
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//check if the file exists or not</span>
                FileStream myFStream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileStream<span style="color: #000000;">&#40;</span>path.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, FileMode.<span style="color: #0000FF;">OpenOrCreate</span>, FileAccess.<span style="color: #0000FF;">ReadWrite</span><span style="color: #000000;">&#41;</span>;
&nbsp;
                SqlBytes fileBytes <span style="color: #008000;">=</span> bytesFile;
&nbsp;
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>isCompressed<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    fileBytes <span style="color: #008000;">=</span> fn_Decompress<span style="color: #000000;">&#40;</span>bytesFile<span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">&#125;</span>
&nbsp;
                <span style="color: #FF0000;">int</span> Length <span style="color: #008000;">=</span> <span style="color: #FF0000;">256</span>;
                <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buffer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#91;</span>Length<span style="color: #000000;">&#93;</span>;
&nbsp;
                Stream readStream <span style="color: #008000;">=</span> fileBytes.<span style="color: #0000FF;">Stream</span>;
&nbsp;
                <span style="color: #FF0000;">int</span> bytesRead <span style="color: #008000;">=</span> readStream.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>buffer, 0, Length<span style="color: #000000;">&#41;</span>;
&nbsp;
                <span style="color: #008080; font-style: italic;">// write the required bytes</span>
                <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>bytesRead <span style="color: #008000;">&gt;</span> 0<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    myFStream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>buffer, 0, bytesRead<span style="color: #000000;">&#41;</span>;
                    bytesRead <span style="color: #008000;">=</span> readStream.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>buffer, 0, Length<span style="color: #000000;">&#41;</span>;
                <span style="color: #000000;">&#125;</span>
&nbsp;
                readStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                myFStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
                returnString <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;File is written successfully&quot;</span>;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                returnString <span style="color: #008000;">=</span> ex.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> SqlString<span style="color: #000000;">&#40;</span>returnString<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Installation time to your SQL Server, You need to register your assembly(.dll) as well as referenced Assembly to SQL Server</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> TestAssembly <span style="color: #993333; font-weight: bold;">SET</span> TRUSTWORTHY <span style="color: #993333; font-weight: bold;">ON</span>
GO
EXEC sp_configure <span style="color: #ff0000;">'clr enabled'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span>;
RECONFIGURE <span style="color: #993333; font-weight: bold;">WITH</span> OVERRIDE;
GO
<span style="color: #993333; font-weight: bold;">CREATE</span> ASSEMBLY <span style="color: #66cc66;">&#91;</span>ICSharpCode<span style="color: #66cc66;">.</span>SharpZipLib<span style="color: #66cc66;">.</span>dll<span style="color: #66cc66;">&#93;</span>
                  <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'D:<span style="color: #000099; font-weight: bold;">\A</span>pplications<span style="color: #000099; font-weight: bold;">\F</span>ileCompressorApp<span style="color: #000099; font-weight: bold;">\C</span>LRCompressionFunctions<span style="color: #000099; font-weight: bold;">\D</span>eployment<span style="color: #000099; font-weight: bold;">\I</span>CSharpCode.SharpZipLib.dll'</span>
                  <span style="color: #993333; font-weight: bold;">WITH</span> PERMISSION_SET <span style="color: #66cc66;">=</span> UNSAFE
GO
<span style="color: #993333; font-weight: bold;">CREATE</span> ASSEMBLY <span style="color: #66cc66;">&#91;</span>CLRCompressionFunctions<span style="color: #66cc66;">&#93;</span>
                  <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'D:<span style="color: #000099; font-weight: bold;">\A</span>pplications<span style="color: #000099; font-weight: bold;">\F</span>ileCompressorApp<span style="color: #000099; font-weight: bold;">\C</span>LRCompressionFunctions<span style="color: #000099; font-weight: bold;">\D</span>eployment<span style="color: #000099; font-weight: bold;">\C</span>LRCompressionFunctions.dll'</span>
                  <span style="color: #993333; font-weight: bold;">WITH</span> PERMISSION_SET <span style="color: #66cc66;">=</span> EXTERNAL_ACCESS
GO</pre></div></div>

<p>PERMISSION_SET = SAFE only if you don&#8217;t want the assembly accessing external resources such as writing to disk, but in this case the function is used to write into the disk<br />
e.g How about if you want to use/register System.Drawing to your assembly? Yes you can do it by using</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> ASSEMBLY <span style="color: #66cc66;">&#91;</span>System<span style="color: #66cc66;">.</span>Drawing<span style="color: #66cc66;">.</span>dll<span style="color: #66cc66;">&#93;</span>
<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'C:<span style="color: #000099; font-weight: bold;">\W</span>INDOWS<span style="color: #000099; font-weight: bold;">\M</span>icrosoft.NET<span style="color: #000099; font-weight: bold;">\F</span>ramework<span style="color: #000099; font-weight: bold;">\v</span>2.0.50727<span style="color: #000099; font-weight: bold;">\S</span>ystem.Drawing.dll'</span>
<span style="color: #993333; font-weight: bold;">WITH</span> PERMISSION_SET <span style="color: #66cc66;">=</span> UNSAFE</pre></div></div>

<p>You need to enable the CLR on SQL server in order to use your function</p>
<p>Now you need to create your function based on your assembly</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>fn_Compress<span style="color: #66cc66;">&#93;</span>           <span style="color: #66cc66;">&#40;</span>
                 @uncompressedBytes varbinary<span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            RETURNS varbinary<span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#41;</span>
            <span style="color: #993333; font-weight: bold;">AS</span>    EXTERNAL NAME CLRCompressionFunctions<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>CLRCompressionFunctions<span style="color: #66cc66;">.</span>CompressionCore<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span>fn_Compress
GO
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>fn_Decompress<span style="color: #66cc66;">&#93;</span>           <span style="color: #66cc66;">&#40;</span>
                 @uncompressedBytes varbinary<span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            RETURNS varbinary<span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#41;</span>
            <span style="color: #993333; font-weight: bold;">AS</span>    EXTERNAL NAME CLRCompressionFunctions<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>CLRCompressionFunctions<span style="color: #66cc66;">.</span>CompressionCore<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span>fn_Decompress
GO
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>fn_WriteFile<span style="color: #66cc66;">&#93;</span>           <span style="color: #66cc66;">&#40;</span>
                 @path nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
				 @bytesFile varbinary<span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
				 @bitCompressed bit<span style="color: #66cc66;">&#41;</span>
			RETURNS nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #993333; font-weight: bold;">AS</span>    EXTERNAL NAME CLRCompressionFunctions<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>CLRCompressionFunctions<span style="color: #66cc66;">.</span>CompressionCore<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span>fn_WriteFile
GO</pre></div></div>

<p>Usage, It&#8217;s the same as you call a function in SQL Server</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>fn_Compress<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>testimage<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> tblImages
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>fn_Decompress<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>imgFileContent<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> TABLE_NAME
GO	
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>fn_WriteFile<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'c:<span style="color: #000099; font-weight: bold;">\t</span>est.pdf'</span><span style="color: #66cc66;">,</span>imgFileContent<span style="color: #66cc66;">,</span> 0<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> TABLE_NAME
GO</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/sql-server-function-using-clr/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Self Inner Join with Group By in SQL Server</title>
		<link>http://fransiscuss.com/self-inner-join-with-group-by-in-sql-server/</link>
		<comments>http://fransiscuss.com/self-inner-join-with-group-by-in-sql-server/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 00:29:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=194</guid>
		<description><![CDATA[This is the case:
I have a table and basically this table has many redundancy. The solution is to create a link table and this table will contains record association. So what I need to do is to get all the ID&#8217;s Populated to the link table and link it to a single unique record on [...]]]></description>
			<content:encoded><![CDATA[<p>This is the case:<br />
I have a table and basically this table has many redundancy. The solution is to create a link table and this table will contains record association. So what I need to do is to get all the ID&#8217;s Populated to the link table and link it to a single unique record on the original table. The logic that I used is to get the value from the column based on the lowest ID.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> tblExpReceiptFileAssociation<span style="color: #66cc66;">&#40;</span>intExpenseDtlId<span style="color: #66cc66;">,</span> intFileID<span style="color: #66cc66;">&#41;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> te<span style="color: #66cc66;">.</span>intExpenseDtlID<span style="color: #66cc66;">,</span> ta<span style="color: #66cc66;">.</span>LinkFileID
	<span style="color: #993333; font-weight: bold;">FROM</span> tblExpReceiptFile te
	<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span>
		<span style="color: #66cc66;">&#40;</span>
			<span style="color: #993333; font-weight: bold;">SELECT</span> vcFileName<span style="color: #66cc66;">,</span> bintFileSize<span style="color: #66cc66;">,</span> chCreateStaffCode<span style="color: #66cc66;">,</span> 
				   CONVERT<span style="color: #66cc66;">&#40;</span>VARCHAR<span style="color: #66cc66;">,</span>sdCreateDate<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">101</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> DateCreated<span style="color: #66cc66;">,</span>
				   MIN<span style="color: #66cc66;">&#40;</span>intFileID<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> LinkFileID
			<span style="color: #993333; font-weight: bold;">FROM</span>
				tblExpReceiptFile
			<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> 
				vcFileName<span style="color: #66cc66;">,</span> bintFileSize<span style="color: #66cc66;">,</span> 
				chCreateStaffCode<span style="color: #66cc66;">,</span> CONVERT<span style="color: #66cc66;">&#40;</span>VARCHAR<span style="color: #66cc66;">,</span>sdCreateDate<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">101</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#41;</span> ta
	<span style="color: #993333; font-weight: bold;">ON</span>
		te<span style="color: #66cc66;">.</span>vcFileName <span style="color: #66cc66;">=</span> ta<span style="color: #66cc66;">.</span>vcFileName
	<span style="color: #993333; font-weight: bold;">AND</span>
		te<span style="color: #66cc66;">.</span>bintFileSize <span style="color: #66cc66;">=</span> ta<span style="color: #66cc66;">.</span>bintFileSize
	<span style="color: #993333; font-weight: bold;">AND</span>
		te<span style="color: #66cc66;">.</span>chCreateStaffCode <span style="color: #66cc66;">=</span> ta<span style="color: #66cc66;">.</span>chCreateStaffCode
	<span style="color: #993333; font-weight: bold;">AND</span>
		CONVERT<span style="color: #66cc66;">&#40;</span>VARCHAR<span style="color: #66cc66;">,</span>te<span style="color: #66cc66;">.</span>sdCreateDate<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">101</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> ta<span style="color: #66cc66;">.</span>DateCreated</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/self-inner-join-with-group-by-in-sql-server/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Linq.Binary(SQL Image Data Type) Type to File Stream</title>
		<link>http://fransiscuss.com/linqbinarysql-image-data-type-type-to-file-stream/</link>
		<comments>http://fransiscuss.com/linqbinarysql-image-data-type-type-to-file-stream/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 23:46:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=192</guid>
		<description><![CDATA[I got a column with Data Type of Image in SQL Server. What I would like to do is to omit this data to a FileStream or to a file. I tried to read the data using LINQ and I found the data type detected for that particular column is System.Data.Linq.Binary. I was expecting it [...]]]></description>
			<content:encoded><![CDATA[<p>I got a column with Data Type of Image in SQL Server. What I would like to do is to omit this data to a FileStream or to a file. I tried to read the data using LINQ and I found the data type detected for that particular column is <strong>System.Data.Linq.Binary</strong>. I was expecting it to be Byte Data type. So I need to convert the Binary to byte then to File Stream. But I found a simpler way by using &#8220;ToArray&#8221; properties and cast it back to byte solves my problem.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="csharp csharp" style="font-family:monospace;">    <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>tblExpReceiptFile file <span style="color: #0600FF;">in</span> ExpReceiptFactory.<span style="color: #0000FF;">tblExpReceiptFileSelect</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">string</span> fileName <span style="color: #008000;">=</span> file.<span style="color: #0000FF;">vcFileName</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span>,<span style="color: #666666;">&quot;_&quot;</span><span style="color: #000000;">&#41;</span>;
                FileStream fileStream <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>DirectoryPath <span style="color: #008000;">+</span> <span style="">@&quot;\&quot;</span> <span style="color: #008000;">+</span> fileName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;.pdf&quot;</span><span style="color: #000000;">&#41;</span>;
                fileStream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>file.<span style="color: #0000FF;">imgFileContent</span>.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, 0, <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>file.<span style="color: #0000FF;">imgFileContent</span>.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span>;
                fileStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/linqbinarysql-image-data-type-type-to-file-stream/feed/</wfw:commentRss>
		</item>
		<item>
		<title>SQL Methods in LINQ</title>
		<link>http://fransiscuss.com/sql-methods-in-linq/</link>
		<comments>http://fransiscuss.com/sql-methods-in-linq/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 06:59:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=190</guid>
		<description><![CDATA[I&#8217;m trying to implement DateDiff in LINQ. I would like to get all records with the same date and ignoring the timestamp.
Here is my code snippet which doesn&#8217;t work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 public int tblExpReceiptFileUniqueID&#40;string chCreateStaffCode, string vcFileName, 
                    [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m trying to implement DateDiff in LINQ. I would like to get all records with the same date and ignoring the timestamp.</p>
<p>Here is my code snippet which doesn&#8217;t work</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp csharp" style="font-family:monospace;"> <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> tblExpReceiptFileUniqueID<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> chCreateStaffCode, <span style="color: #FF0000;">string</span> vcFileName, 
                                                <span style="color: #FF0000;">long</span> intFileSize, DateTime<span style="color: #008000;">?</span> sdCreateDate<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span> 
            var expReceiptFile <span style="color: #008000;">=</span> from ef <span style="color: #0600FF;">in</span> DataContext.<span style="color: #0000FF;">tblExpReceiptFiles</span> orderby ef.<span style="color: #0000FF;">intFileID</span> 
                                 where ef.<span style="color: #0000FF;">chCreateStaffCode</span> <span style="color: #008000;">==</span> chCreateStaffCode
                                 <span style="color: #008000;">&amp;&amp;</span> ef.<span style="color: #0000FF;">vcFileName</span> <span style="color: #008000;">==</span> vcFileName 
                                 <span style="color: #008000;">&amp;&amp;</span> ef.<span style="color: #0000FF;">bintFileSize</span> <span style="color: #008000;">==</span> intFileSize
                                 <span style="color: #008000;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>DateTime<span style="color: #000000;">&#41;</span>ef.<span style="color: #0000FF;">sdCreateDate</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Date</span> <span style="color: #008000;">==</span> sdCreateDate.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">Date</span>
                                 select ef;
&nbsp;
            tblExpReceiptFile expReceiptFileRec <span style="color: #008000;">=</span> expReceiptFile.<span style="color: #0000FF;">First</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">return</span> expReceiptFileRec.<span style="color: #0000FF;">intFileID</span>;
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;m trying to cast the column to &#8220;DateTime&#8221; and use the &#8220;Date&#8221; property but I got this error<br />
<strong>&#8220;The Member  &#8216;System.DateTime.Date&#8217; has no supported translation to SQL&#8221;</strong></p>
<p>I thought of &#8220;DATEDIFF&#8221; function in SQL server and I&#8217;m trying to get my head around in implementing this using LINQ and I found that we can use a library called &#8220;SQLClient&#8221;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data.Linq.SqlClient</span>;</pre></td></tr></table></div>

<p>Code Snippet which is working</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> tblExpReceiptFileUniqueID<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> chCreateStaffCode, <span style="color: #FF0000;">string</span> vcFileName, 
                                                <span style="color: #FF0000;">long</span> intFileSize, DateTime<span style="color: #008000;">?</span> sdCreateDate<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span> 
            var expReceiptFile <span style="color: #008000;">=</span> from ef <span style="color: #0600FF;">in</span> DataContext.<span style="color: #0000FF;">tblExpReceiptFiles</span> orderby ef.<span style="color: #0000FF;">intFileID</span> 
                                 where ef.<span style="color: #0000FF;">chCreateStaffCode</span> <span style="color: #008000;">==</span> chCreateStaffCode
                                 <span style="color: #008000;">&amp;&amp;</span> ef.<span style="color: #0000FF;">vcFileName</span> <span style="color: #008000;">==</span> vcFileName 
                                 <span style="color: #008000;">&amp;&amp;</span> ef.<span style="color: #0000FF;">bintFileSize</span> <span style="color: #008000;">==</span> intFileSize
                                 <span style="color: #008000;">&amp;&amp;</span> SqlMethods.<span style="color: #0000FF;">DateDiffDay</span><span style="color: #000000;">&#40;</span>ef.<span style="color: #0000FF;">sdCreateDate</span>, sdCreateDate.<span style="color: #0000FF;">Value</span>.<span style="color: #0000FF;">Date</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> 0
                                 select ef;
&nbsp;
            tblExpReceiptFile expReceiptFileRec <span style="color: #008000;">=</span> expReceiptFile.<span style="color: #0000FF;">First</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #0600FF;">return</span> expReceiptFileRec.<span style="color: #0000FF;">intFileID</span>;
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/sql-methods-in-linq/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Check Existence of temp table in memory</title>
		<link>http://fransiscuss.com/check-existence-of-temp-table-in-memory/</link>
		<comments>http://fransiscuss.com/check-existence-of-temp-table-in-memory/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 22:36:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=185</guid>
		<description><![CDATA[How to check whether the temp table is exists on the memory or not?The reason why you need this is because your stored procedure can throw the error when you try to drop a temp table which is not exists anymore on the memory. The best practice to drop a temp table is normally to [...]]]></description>
			<content:encoded><![CDATA[<p>How to check whether the temp table is exists on the memory or not?The reason why you need this is because your stored procedure can throw the error when you try to drop a temp table which is not exists anymore on the memory. The best practice to drop a temp table is normally to check the existence of the table on the memory then we drop the table if it is exists</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">--Drop the table after usage</span>
<span style="color: #993333; font-weight: bold;">IF</span> object_id<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'tempdb..#tmpStandardCostRate'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
BEGIN
   <span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #808080; font-style: italic;">#tmpStandardCostRate</span>
END</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/check-existence-of-temp-table-in-memory/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Find Index in all the tables SQL Server</title>
		<link>http://fransiscuss.com/find-index-in-all-the-tables-sql-server/</link>
		<comments>http://fransiscuss.com/find-index-in-all-the-tables-sql-server/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 05:39:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=179</guid>
		<description><![CDATA[This is a query to find all the indexes in your table including when it was last updated
Find all indexes on all tables

1
2
3
4
5
6
7
8
9
SELECT 
	OBJECT_NAME&#40;OBJECT_ID&#41; AS 'Table Name', 
	&#91;name&#93; AS 'Statistic', 
	STATS_DATE&#40;object_id, index_id&#41; AS 'Last Updated Statistics Date'
FROM
	sys.indexes
ORDER BY
	 STATS_DATE&#40;object_id, index_id&#41;
DESC

Find all indexes on a particular table

1
2
3
4
5
6
7
8
SELECT 
	OBJECT_NAME&#40;OBJECT_ID&#41; AS 'Table Name', 
	&#91;name&#93; AS 'Statistic', 
	STATS_DATE&#40;object_id, index_id&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>This is a query to find all the indexes in your table including when it was last updated</p>
<p><strong>Find all indexes on all tables</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> 
	OBJECT_NAME<span style="color: #66cc66;">&#40;</span>OBJECT_ID<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'Table Name'</span><span style="color: #66cc66;">,</span> 
	<span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'Statistic'</span><span style="color: #66cc66;">,</span> 
	STATS_DATE<span style="color: #66cc66;">&#40;</span>object_id<span style="color: #66cc66;">,</span> index_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'Last Updated Statistics Date'</span>
<span style="color: #993333; font-weight: bold;">FROM</span>
	sys<span style="color: #66cc66;">.</span>indexes
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span>
	 STATS_DATE<span style="color: #66cc66;">&#40;</span>object_id<span style="color: #66cc66;">,</span> index_id<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">DESC</span></pre></td></tr></table></div>

<p><strong>Find all indexes on a particular table</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> 
	OBJECT_NAME<span style="color: #66cc66;">&#40;</span>OBJECT_ID<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'Table Name'</span><span style="color: #66cc66;">,</span> 
	<span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'Statistic'</span><span style="color: #66cc66;">,</span> 
	STATS_DATE<span style="color: #66cc66;">&#40;</span>object_id<span style="color: #66cc66;">,</span> index_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'Last Updated Statistics Date'</span>
<span style="color: #993333; font-weight: bold;">FROM</span>
	sys<span style="color: #66cc66;">.</span>indexes
<span style="color: #993333; font-weight: bold;">WHERE</span> 
        OBJECT_NAME<span style="color: #66cc66;">&#40;</span>OBJECT_ID<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'YourTableName'</span></pre></td></tr></table></div>

<p><strong> This SQL query returning the information about number of rows in the table as well as number of update/insert/delete after the last index has been rebuilt</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> 
	<span style="color: #ff0000;">'TABLE '</span> <span style="color: #66cc66;">=</span> substring<span style="color: #66cc66;">&#40;</span>sysobjects<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' INDEX '</span> <span style="color: #66cc66;">=</span> substring<span style="color: #66cc66;">&#40;</span>sysindexes<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">,</span>sysIndexes<span style="color: #66cc66;">.</span>rowcnt<span style="color: #66cc66;">,</span> sysindexes<span style="color: #66cc66;">.</span>rowmodctr
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>last updated<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">=</span>STATS_DATE<span style="color: #66cc66;">&#40;</span>sysobjects<span style="color: #66cc66;">.</span>id<span style="color: #66cc66;">,</span> sysindexes<span style="color: #66cc66;">.</span>indid<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">,</span>user_seeks<span style="color: #66cc66;">,</span>user_scans<span style="color: #66cc66;">,</span>user_lookups<span style="color: #66cc66;">,</span>user_updates
<span style="color: #993333; font-weight: bold;">FROM</span>	sysobjects
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> sysindexes <span style="color: #993333; font-weight: bold;">ON</span> sysobjects<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">=</span> sysindexes<span style="color: #66cc66;">.</span>id  <span style="color: #993333; font-weight: bold;">AND</span> sysindexes<span style="color: #66cc66;">.</span>indid <span style="color: #66cc66;">&gt;</span> 0 
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>dm_db_index_usage_stats iusage <span style="color: #993333; font-weight: bold;">ON</span> iusage<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> sysobjects<span style="color: #66cc66;">.</span>id  <span style="color: #993333; font-weight: bold;">AND</span> iusage<span style="color: #66cc66;">.</span>index_id <span style="color: #66cc66;">=</span> sysindexes<span style="color: #66cc66;">.</span>indid
<span style="color: #993333; font-weight: bold;">WHERE</span>	
	sysobjects<span style="color: #66cc66;">.</span>xtype <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'U'</span> 
<span style="color: #993333; font-weight: bold;">AND</span> 
	iusage<span style="color: #66cc66;">.</span>database_id <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> dbid <span style="color: #993333; font-weight: bold;">FROM</span> master<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>sysdatabases <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> db_name<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> sysobjects<span style="color: #66cc66;">.</span>name</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/find-index-in-all-the-tables-sql-server/feed/</wfw:commentRss>
		</item>
		<item>
		<title>CSV parsing in SQL server</title>
		<link>http://fransiscuss.com/csv-parsing-in-sql-server/</link>
		<comments>http://fransiscuss.com/csv-parsing-in-sql-server/#comments</comments>
		<pubDate>Mon, 01 Sep 2008 01:13:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=157</guid>
		<description><![CDATA[I&#8217;ve got this SQL Function to parse comma separated value into a table. We need this function when our application commit separated comma value in a text. We normally do this when we want to send an array of value to the SQL server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
CREATE FUNCTION &#91;dbo&#93;.&#91;SplitCSV&#93;&#40;@CSV text&#41;
-- Returns a table with txtValue column
RETURNS @OutTable TABLE&#40;txtValue [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got this SQL Function to parse comma separated value into a table. We need this function when our application commit separated comma value in a text. We normally do this when we want to send an array of value to the SQL server</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>SplitCSV<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>@CSV text<span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">-- Returns a table with txtValue column</span>
RETURNS @OutTable <span style="color: #993333; font-weight: bold;">TABLE</span><span style="color: #66cc66;">&#40;</span>txtValue text<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">AS</span>
BEGIN
&nbsp;
declare @currentposition int
declare @nextposition    int
declare @lengthOfString  int  
&nbsp;
<span style="color: #808080; font-style: italic;">-- Assign the starting position of the string</span>
<span style="color: #993333; font-weight: bold;">SET</span> @currentposition <span style="color: #66cc66;">=</span> 0
 <span style="color: #808080; font-style: italic;">-- The reason for this is to force entrance into the while loop below.</span>
<span style="color: #993333; font-weight: bold;">SET</span> @nextposition <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>             
&nbsp;
WHILE @nextposition <span style="color: #66cc66;">&gt;</span> 0
BEGIN
<span style="color: #808080; font-style: italic;">-- Assign the next position to be the current index of ',' + 1</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> @nextposition <span style="color: #66cc66;">=</span> charindex<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">','</span><span style="color: #66cc66;">,</span> @CSV<span style="color: #66cc66;">,</span> @currentposition <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> 
&nbsp;
<span style="color: #808080; font-style: italic;">-- In here we need to find 2 things. The position of the ','</span>
<span style="color: #808080; font-style: italic;">-- and the length of the string segment in between.</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> @lengthOfString <span style="color: #66cc66;">=</span> CASE WHEN @nextposition <span style="color: #66cc66;">&gt;</span> 0
       THEN @nextposition
       ELSE DATALENGTH<span style="color: #66cc66;">&#40;</span>@CSV<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>
       END <span style="color: #66cc66;">-</span> @currentposition <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span>
&nbsp;
<span style="color: #808080; font-style: italic;">--After the length and position is found all we need to do</span>
<span style="color: #808080; font-style: italic;">--is take the substring of the string passed in.</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> @OutTable <span style="color: #66cc66;">&#40;</span>txtValue<span style="color: #66cc66;">&#41;</span>
       <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>substring<span style="color: #66cc66;">&#40;</span>@CSV<span style="color: #66cc66;">,</span> @currentposition <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> @lengthOfString<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">--Set the current position to the next position</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> @currentposition <span style="color: #66cc66;">=</span> @nextposition
END
<span style="color: #993333; font-weight: bold;">RETURN</span>
END</pre></td></tr></table></div>

<p><strong>Usage in SQL Query</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="sql sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span>
	VenueID
<span style="color: #993333; font-weight: bold;">FROM</span>
	Venues
<span style="color: #993333; font-weight: bold;">WHERE</span> 
	PostCode
<span style="color: #993333; font-weight: bold;">IN</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> CAST<span style="color: #66cc66;">&#40;</span>txtValue <span style="color: #993333; font-weight: bold;">AS</span> VarChar<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> dbo<span style="color: #66cc66;">.</span>splitcsv<span style="color: #66cc66;">&#40;</span>@PostCode<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/csv-parsing-in-sql-server/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
