<?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>Tue, 17 Aug 2010 05:28:50 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>Read and Update XML Node of XML datatype in SQL Server</title>
		<link>http://fransiscuss.com/read-and-update-xml-node-of-xml-datatype-in-sql-server/</link>
		<comments>http://fransiscuss.com/read-and-update-xml-node-of-xml-datatype-in-sql-server/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 00:04:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

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

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

		<guid isPermaLink="false">http://fransiscuss.com/?p=265</guid>
		<description><![CDATA[
Select Node value

This is a simple explanation on how to select a particular node from xml data type of a record in SQL server.
Sample XML

&#60;iReference&#62;
  &#60;chAuditorCode IsChanged=&#34;false&#34;&#62;00003479&#60;/chAuditorCode&#62;
  &#60;vcAuditorDesc IsChanged=&#34;true&#34;&#62;Brodie's HomeSton&#60;/vcAuditorDesc&#62;
  &#60;sdTermDate IsChanged=&#34;false&#34;&#62;NULL&#60;/sdTermDate&#62;
  &#60;Action /&#62;
  &#60;Comment /&#62;
  &#60;chUpdateStaffCode&#62;AU941170&#60;/chUpdateStaffCode&#62;
  &#60;dtUpdateDateTime&#62;2010-07-22T09:42:20&#60;/dtUpdateDateTime&#62;
  &#60;ChangedColumns&#62;vcAuditorDesc,chUpdateStaffCode,dtUpdateDateTime,dtUpdateDateTime&#60;/ChangedColumns&#62;
&#60;/iReference&#62;

So what I want to do is basically to [...]]]></description>
			<content:encoded><![CDATA[<p><strong><br />
Select Node value<br />
</strong><br />
This is a simple explanation on how to select a particular node from xml data type of a record in SQL server.</p>
<p>Sample XML</p>

<div class="wp_syntax"><div class="code"><pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;iReference<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;chAuditorCode</span> <span style="color: #000066;">IsChanged</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>00003479<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/chAuditorCode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;vcAuditorDesc</span> <span style="color: #000066;">IsChanged</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Brodie's HomeSton<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/vcAuditorDesc<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sdTermDate</span> <span style="color: #000066;">IsChanged</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>NULL<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sdTermDate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Action</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Comment</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;chUpdateStaffCode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>AU941170<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/chUpdateStaffCode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dtUpdateDateTime<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2010-07-22T09:42:20<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dtUpdateDateTime<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ChangedColumns<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>vcAuditorDesc,chUpdateStaffCode,dtUpdateDateTime,dtUpdateDateTime<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ChangedColumns<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/iReference<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So what I want to do is basically to get the value of ChangedColumns node</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>  xmlDetail<span style="color: #66cc66;">.</span>value<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/iReference[1]/ChangedColumns[1]/.'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'VARCHAR(255)'</span><span style="color: #66cc66;">&#41;</span>  
<span style="color: #993333; font-weight: bold;">FROM</span> tblReferenceHistory
<span style="color: #993333; font-weight: bold;">WHERE</span> intReferenceHistoryID <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">125</span></pre></div></div>

<p><strong><br />
Update Existing Node value<br />
</strong></p>
<p>Sample XML</p>

<div class="wp_syntax"><div class="code"><pre class="xml xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;iReference<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;chAuditorCode</span> <span style="color: #000066;">IsChanged</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>00003479<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/chAuditorCode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;vcAuditorDesc</span> <span style="color: #000066;">IsChanged</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Brodie's HomeSton<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/vcAuditorDesc<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sdTermDate</span> <span style="color: #000066;">IsChanged</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>NULL<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sdTermDate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Action</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Comment</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;chUpdateStaffCode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>AU941170<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/chUpdateStaffCode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dtUpdateDateTime<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2010-07-22T09:42:20<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dtUpdateDateTime<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ChangedColumns</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/iReference<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Changed column has no value in it and you want to update it. Basically you can&#8217;t edit the existing node value so what you need to do is to readd it and remove the first instance of that node</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;">DECLARE @ChangedColumns VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> @ChangedColumns <span style="color: #66cc66;">=</span> xmlDetail<span style="color: #66cc66;">.</span>value<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/iReference[1]/ChangedColumns[1]/.'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'VARCHAR(255)'</span><span style="color: #66cc66;">&#41;</span>  
<span style="color: #993333; font-weight: bold;">FROM</span> tblReferenceHistory
<span style="color: #993333; font-weight: bold;">WHERE</span> intReferenceHistoryID <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">125</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- first update - add a new &lt;ChangedColumns&gt;...&lt;/ChangedColumns&gt; node    </span>
<span style="color: #993333; font-weight: bold;">UPDATE</span> tblReferenceHistory
<span style="color: #993333; font-weight: bold;">SET</span> xmlDetail<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">MODIFY</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'insert &lt;ChangedColumns&gt;{sql:variable(&quot;@ChangedColumns&quot;)}&lt;/ChangedColumns&gt; as last into (/iReference)[1]'</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #993333; font-weight: bold;">WHERE</span> intReferenceHistoryID <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">124</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- second update - remove the empty &lt;ChangedColumns /&gt; node    </span>
<span style="color: #993333; font-weight: bold;">UPDATE</span> dbo<span style="color: #66cc66;">.</span>tblReferenceHistory
<span style="color: #993333; font-weight: bold;">SET</span> xmlDetail<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">MODIFY</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'delete (/iReference[1]/ChangedColumns[1])[1]'</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #993333; font-weight: bold;">WHERE</span> intReferenceHistoryID <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">124</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> xmlDetail<span style="color: #66cc66;">.</span>value<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/iReference[1]/ChangedColumns[1]/.'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'VARCHAR(255)'</span><span style="color: #66cc66;">&#41;</span>  
<span style="color: #993333; font-weight: bold;">FROM</span> tblReferenceHistory
<span style="color: #993333; font-weight: bold;">WHERE</span> intReferenceHistoryID <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">124</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/read-and-update-xml-node-of-xml-datatype-in-sql-server/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to set variable from Dynamic SQL</title>
		<link>http://fransiscuss.com/how-to-set-variable-from-dynamic-sql/</link>
		<comments>http://fransiscuss.com/how-to-set-variable-from-dynamic-sql/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 05:07:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

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

		<guid isPermaLink="false">http://fransiscuss.com/?p=259</guid>
		<description><![CDATA[Here is the scenario, You have a stored procedure that builds dynamic insert SQL which means the &#8220;INSERT&#8221; statement is being composed on the fly based on the table name but at the same time you need to get SCOPE_IDENTITY or the last identity number inserted for further processing (e.g History table)

	DECLARE @nvcSQL			NVARCHAR&#40;MAX&#41;	
	DECLARE @pvcTableName	VARCHAR&#40;255&#41;
	DECLARE @pvcColumn		VARCHAR&#40;255&#41;
	DECLARE [...]]]></description>
			<content:encoded><![CDATA[<p>Here is the scenario, You have a stored procedure that builds dynamic insert SQL which means the &#8220;INSERT&#8221; statement is being composed on the fly based on the table name but at the same time you need to get SCOPE_IDENTITY or the last identity number inserted for further processing (e.g History table)</p>

<div class="wp_syntax"><div class="code"><pre class="sql sql" style="font-family:monospace;">	DECLARE @nvcSQL			NVARCHAR<span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#41;</span>	
	DECLARE @pvcTableName	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
	DECLARE @pvcColumn		VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
	DECLARE @pvcValue		NVARCHAR<span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">--This is used to actually get the primary key for identity record</span>
	DECLARE @dvcPrimaryKey VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> 
	<span style="color: #993333; font-weight: bold;">SET</span> @dvcPrimaryKey <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">''</span>
&nbsp;
	DECLARE @dintPrimaryKey INT	
	<span style="color: #993333; font-weight: bold;">SELECT</span> @dvcPrimaryKey <span style="color: #66cc66;">=</span> ISNULL<span style="color: #66cc66;">&#40;</span>NAME<span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> SYS<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">COLUMNS</span> <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> @pvcTableName <span style="color: #993333; font-weight: bold;">AND</span> Is_Identity <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> 
&nbsp;
&nbsp;
	<span style="color: #808080; font-style: italic;">-- Only execute when there is a @pvcValue.</span>
	<span style="color: #993333; font-weight: bold;">IF</span> @pvcValue <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #ff0000;">''</span>
	BEGIN
		<span style="color: #993333; font-weight: bold;">SELECT</span> @nvcSQL <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'INSERT INTO '</span> <span style="color: #66cc66;">+</span> @pvcTableName <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'('</span> <span style="color: #66cc66;">+</span> @pvcColumn<span style="color: #66cc66;">+</span> <span style="color: #ff0000;">') VALUES ('</span> <span style="color: #66cc66;">+</span> @pvcValue <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">')'</span>
		<span style="color: #993333; font-weight: bold;">SELECT</span> @nvcSQL <span style="color: #66cc66;">=</span> @nvcSQL <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">';'</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'SELECT @dintPrimaryKey = SCOPE_IDENTITY()'</span>
		<span style="color: #808080; font-style: italic;">--PRINT @nvcSQL</span>
&nbsp;
		EXEC sp_executesql @query <span style="color: #66cc66;">=</span> @nvcSQL<span style="color: #66cc66;">,</span> @params <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">'@dintPrimaryKey INT OUTPUT'</span><span style="color: #66cc66;">,</span> @dintPrimaryKey <span style="color: #66cc66;">=</span> @dintPrimaryKey OUTPUT 
		<span style="color: #808080; font-style: italic;">--EXEC (@nvcSQL)</span>
	END
&nbsp;
	<span style="color: #993333; font-weight: bold;">IF</span> @dvcPrimaryKey <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #ff0000;">''</span>
	BEGIN
		<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">&#40;</span>@dvcPrimaryKey <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">' = '</span> <span style="color: #66cc66;">+</span> CAST<span style="color: #66cc66;">&#40;</span>@dintPrimaryKey <span style="color: #993333; font-weight: bold;">AS</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #993333; font-weight: bold;">AS</span> PrimaryKey 
	END
	ELSE
	BEGIN
		<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">''</span> <span style="color: #993333; font-weight: bold;">AS</span> PrimaryKey
	END</pre></div></div>

<p>Question: why do we need to use sp_executesql instead of EXEC?because EXEC just execute a SQL command without returning/expecting any result from it</p>
]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/how-to-set-variable-from-dynamic-sql/feed/</wfw:commentRss>
		</item>
		<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>
	</channel>
</rss>
