<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL Server with Mr. Denny &#187; SQL CLR Procedure</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/sql-server/tag/sql-clr-procedure/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/sql-server</link>
	<description></description>
	<lastBuildDate>Mon, 10 Jun 2013 17:25:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>When does SQLCLR launch?</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/when-does-sqlclr-launch/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/when-does-sqlclr-launch/#comments</comments>
		<pubDate>Thu, 14 Jun 2012 21:00:00 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[CLR]]></category>
		<category><![CDATA[SQL CLR Procedure]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/when-does-sqlclr-launch/</guid>
		<description><![CDATA[A change was made to the SQLCLR, as to when the SQLCLR is loaded.&#160; In SQL Server 2008 R2 and below the SQLCLR component was loaded only when the “clr enabled” setting, which is controlled through sp_configure, is enabled.&#160; Even with the SQLCLR is enabled on SQL Server 2008 R2 and below, the SQLCLR is [...]]]></description>
				<content:encoded><![CDATA[<p>A change was made to the SQLCLR, as to when the SQLCLR is loaded.&#160; In SQL Server 2008 R2 and below the SQLCLR component was loaded only when the “clr enabled” setting, which is controlled through sp_configure, is enabled.&#160; Even with the SQLCLR is enabled on SQL Server 2008 R2 and below, the SQLCLR is only loaded into SQL Server’s memory when it is needed.</p>
<p>In SQL Server 2012 however SQLCLR is loaded on startup no matter if the “clr enabled” setting is disabled or not.&#160; This can be seen in the SQL Server ERRORLOG file by looking for the line which looks similar to the following.</p>
<blockquote><p>Common language runtime (CLR) functionality initialized using CLR version v4.0.30319 from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\.’</p>
</blockquote>
<p>There are a couple of reasons that Microsoft wants the SQLCLR to be loaded automatically when the SQL Server starts instead of waiting for permission to load it.</p>
<p>The first is that there are plenty of native features which require the use of the SQLCLR in SQL Server 2012.&#160; This includes features like the geometry data type, the hierarchyid data type, etc.</p>
<p>The second is that by loading the SQLCLR when the SQL Server instance first starts this decreases the time that SQL Server must wait the first time that the SQLCLR is used. This is because the SQLCLR is a rather large package which must be loaded, when loading the SQLCLR the first time it is used can take up to several seconds to load the CLR into memory for use.</p>
<p>Just because the SQLCLR is loaded into memory when the SQL Server instance is first started doesn’t mean that the sp_configure setting “clr enabled” is useless.&#160; With this setting disabled, the SQL Server instance will not allow for the creation of CLR objects.&#160; Once the “clr enabled” setting is enabled the CLR doesn’t need to be loaded, but the SQL Server instance will now allow the user created assemblies to be created within the SQL Server instance.</p>
<p>Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/when-does-sqlclr-launch/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SQL Server 2012, SQLCLR and Max Server Memory</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/sql-server-2012-sqlclr-and-max-server-memory/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/sql-server-2012-sqlclr-and-max-server-memory/#comments</comments>
		<pubDate>Mon, 11 Jun 2012 21:00:00 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[CLR]]></category>
		<category><![CDATA[SQL CLR Procedure]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/sql-server-2012-sqlclr-and-max-server-memory/</guid>
		<description><![CDATA[When SQLCLR was first introduced in SQL Server 2005, one of the big complaints that I had with it was that SQLCLR could technically steal all the memory away from the SQL Server’s buffer pool by simply growing it’s memory pool which would cause the OS to request memory back from the SQL Server taking [...]]]></description>
				<content:encoded><![CDATA[<p>When SQLCLR was first introduced in SQL Server 2005, one of the big complaints that I had with it was that SQLCLR could technically steal all the memory away from the SQL Server’s buffer pool by simply growing it’s memory pool which would cause the OS to request memory back from the SQL Server taking memory away from the buffer pool and the execution plan cache.&#160; This could result, depending on the amount of memory that the SQLCLR needed, the entire memory for the server being eaten up by the SQLCLR component of Microsoft SQL Server, eventually possibly taking all the memory on the entire server for the SQLCLR component.</p>
<p>With the release of SQL Server 2012 this problem goes away to some extent as SQL Server 2012 had many memory architecture related changes put into the product.&#160; Among the variety of changes which were introduced in SQL Server 2012 is the fact that the SQLCLR feature now gets its memory via the traditional SQL Server memory manager.&#160; One of the side effects of this is that the SQLCLR how falls under the control of the “max server memory” setting which is accessed through the sp_configure system stored procedure.</p>
<p>While this doesn’t give you the ability to control how much memory the SQLCLR component specially will take this does mean that you can via the “max server memory” setting keep the SQLCLR processes from taking over all the memory within the Windows OS.</p>
<p>Because of this change in how memory is controlled for the SQLCLR component when upgrading from a prior version of Microsoft SQL Server to SQL Server 2012&#160; the value which is used for the “max server memory” setting will need to be re-thought out as we no longer need to leave room outside the setting for the SQLCLR.&#160; As an example, if a Windows Server with a single instance of Microsoft SQL Server 2008 had 64 Gigs of RAM installed and the instance was a heavy user of SQLCLR it might have made sense to configure the SQL Server instance with 50 Gigs of memory as the max server memory leaving about 4 Gigs for the Windows OS, and 10 Gigs for the SQLCLR.&#160; When this server is upgraded to SQL Server 2012 you would then see that the memory usage for the Windows OS would drop leaving a large amount of memory un-used as the memory which was before allocated outside of the “max server memory” setting will sit there un-used by the SQLCLR.&#160; As this memory is within the “max server memory” setting the buffer pool and procedure cache will now be reduced by the amount of memory that the SQLCLR needs to used below the 50 Gig limit, potentially reducing the amount of memory by 10 Gigs of more.</p>
<p>The upside to this configuration change of the way that the SQLCLR memory is managed is that if the SQLCLR only needs 1 Gig, and before it was being allocated 10 Gigs of memory (as discussed in the example above), it now only takes 1 Gig of memory.&#160; However on the down side is that there is no knob to turn to limit the amount of memory which the SQLCLR can use at the high end.</p>
<p>Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/sql-server-2012-sqlclr-and-max-server-memory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Experience with putting a CRL Procedure into SQL</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/my-experience-with-putting-a-crl-procedure-into-sql/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/my-experience-with-putting-a-crl-procedure-into-sql/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 11:00:26 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[CREATE ASSEMBLY]]></category>
		<category><![CDATA[CREATE PROCEDURE]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Microsoft.SqlServer.Server]]></category>
		<category><![CDATA[SQL CLR Procedure]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server stored procedures]]></category>
		<category><![CDATA[T/SQL]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/my-experience-with-putting-a-crl-procedure-into-sql/</guid>
		<description><![CDATA[We are looking at how to create a C# method which can then be attached to the SQL Server as a stored procedure and called.  To make it a little cooler we are passing data into the procedure and having the C# method return the data through an output parameter.]]></description>
				<content:encoded><![CDATA[<p>A little while ago I came upon a situation where I needed to put a CRL procedure into SQL Server 2005.  Now I&#8217;ve been using SQL Server 2005 since the day it went RTM, and this is the first occasion that I&#8217;ve needed to put a CLR procedure into SQL.  Could I have done what I needed to without a CRL procedure?  Yeah probably, but the .NET code was written, and had been working.</p>
<p>My code is written in C# as that&#8217;s what the .NET guys here use.  There is one method within the C# class called HashPassword.  It takes a text string and makes a SHA1 Hash of it which we then store.  We had to make a couple of changes to the C# code to make it work within SQL Server.</p>
<p>The original code looked like this.<br />
<code><br />
<font size="2" color="#0000ff">using System;<br />
</font><font size="2" color="#0000ff">using System.Collections.Generic;<br />
</font><font size="2" color="#0000ff">using System.IO;<br />
</font><font size="2" color="#0000ff">using System.Security.Cryptography;<br />
</font><font size="2"><font color="#0000ff">using System.Text;<br />
</font></font></code><code><font size="2"><font color="#0000ff"> </font></font></code></p>
<p><code><font size="2"><font color="#0000ff">namespace</font> Rapid.Database.Security{<br />
<font color="#0000ff">public</font> <font color="#0000ff">class</font> <font color="#2b91af">User</font><br />
</font></code><font size="2" face="Courier New">     {</font><br />
<code><font size="2" color="#0000ff">     public</font><font size="2"> </font><font size="2" color="#0000ff">static</font><font size="2"> </font><font size="2" color="#0000ff">void</font><font size="2"> HashPassword(</font><font size="2" color="#2b91af">String</font><font size="2"> password, </font><font size="2" color="#0000ff">out</font><font size="2"> </font><font size="2" color="#2b91af">String</font><font size="2"> hash)</font></code><br />
<code></code><code><font size="2">          {<br />
<font size="2" color="#2b91af">          SHA1</font><font size="2"> sha1 = </font><font size="2" color="#0000ff">new</font><font size="2"> </font><font size="2" color="#2b91af">SHA1CryptoServiceProvider</font><font size="2">();hash = </font><br />
</font><font size="2" color="#2b91af">          BitConverter</font><font size="2">.ToString(sha1.ComputeHash(</font><font size="2" color="#2b91af">UnicodeEncoding</font><font size="2">.Default.GetBytes(password.ToLower()))).Replace(</font><font size="2" color="#a31515">"-"</font><font size="2">, </font><font size="2" color="#a31515">""</font><font size="2">);</font></code><br />
<code><font size="2">          }</font></code><br />
<code><font size="2">     <font size="2">}<br />
}</font></font></code><font size="2"><font size="2"> </font></font></p>
<p><font size="2"><font size="2">The changed C# code looks like this.</font></font></p>
<p><font size="2" color="#0000ff" face="Courier New">using System;<br />
</font><font size="2" color="#0000ff" face="Courier New">using System.Collections.Generic;<br />
</font><font size="2" color="#0000ff" face="Courier New">using System.IO;<br />
</font><font size="2" color="#0000ff" face="Courier New">using System.Security.Cryptography;<br />
</font><font size="2"><font face="Courier New"><font color="#0000ff">using System.Text;<br />
</font><strong><font color="#0000ff">using</font> Microsoft.SqlServer.Server;</strong> </font></font></p>
<p><font size="2"><font face="Courier New"><font color="#0000ff">namespace</font> Rapid.Database.Security</font></font></p>
<p><font size="2"><font face="Courier New">{<br />
<font color="#0000ff">public</font> <font color="#0000ff">class</font> <font color="#2b91af">User</font><br />
     {</font></font></p>
<p><font size="2" face="Courier New"><strong>     [<font color="#0000ff"><font color="#2b91af">SqlProcedure</font></font><font color="#000000">(Name=<font color="#a31515">"HashPassword"</font></font><font color="#000000">)]</font></strong></font><br />
<code><font size="2" color="#0000ff">     public</font><font size="2"> </font><font size="2" color="#0000ff">static</font><font size="2"> </font><font size="2" color="#0000ff">void</font><font size="2"> HashPassword(</font><font size="2" color="#2b91af">String</font><font size="2"> password, </font><font size="2" color="#0000ff">out</font><font size="2"> </font><font size="2" color="#2b91af">String</font><font size="2"> hash)</font></code><br />
<code></code><code><font size="2">          {<br />
<font size="2" color="#2b91af">          SHA1</font><font size="2"> sha1 = </font><font size="2" color="#0000ff">new</font><font size="2"> </font><font size="2" color="#2b91af">SHA1CryptoServiceProvider</font><font size="2">();hash = </font><br />
</font><font size="2" color="#2b91af">          BitConverter</font><font size="2">.ToString(sha1.ComputeHash(</font><font size="2" color="#2b91af">UnicodeEncoding</font><font size="2">.Default.GetBytes(password.ToLower()))).Replace(</font><font size="2" color="#a31515">"-"</font><font size="2">, </font><font size="2" color="#a31515">""</font><font size="2">);</font></code><br />
<code><font size="2">          }</font></code><br />
<code><font size="2">     <font size="2">}<br />
}</font></font></code><font size="2"><font size="2"> </font></font></p>
<p><font size="2">Once these changes are made and the DLL recompiled we can attach the DLL to the SQL Server database as an assembly.  This is done with the CREATE ASSEMBLY command.  I have to use the EXTERNAL_ACCESS flag instead of the SAFE flag because my dll requires the use of the System.IO assembly which can&#8217;t be run as SAFE under SQL Server 2005.</font></p>
<p><font size="2" color="#0000ff">CREATE<font size="2"> </font><font size="2" color="#0000ff">ASSEMBLY</font><font size="2"> [Rapid.Database.Security]</font></font><font size="2" color="#0000ff"><font size="2" color="#0000ff">AUTHORIZATION<font size="2"> [dbo]</font></font><br />
</font><font size="2" color="#0000ff">FROM<font size="2"> </font><font size="2" color="#ff0000">&#8216;D:\Rapid.Database.Security.dll&#8217;</font><br />
</font><font size="2" color="#0000ff"><font size="2" color="#0000ff">WITH</font><font size="2"> </font><font size="2" color="#0000ff">PERMISSION_SET</font><font size="2"> </font><font size="2" color="#808080">=</font><font size="2"> </font><font size="2" color="#0000ff">EXTERNAL_ACCESS</font><br />
</font><font size="2" color="#0000ff">GO</font></p>
<p>We can now create our procedure which needs to have one input and one output parameter to match the number of parameters within the C# procedure.  This is done with the CREATE PROCEDURE command just like creating any normal procedure.</p>
<p><font size="2" color="#0000ff">CREATE</font><font size="2"> </font><font size="2" color="#0000ff">PROCEDURE</font><font size="2"> [dbo]</font><font size="2" color="#808080">.</font><font size="2">[HashPassword]</font><br />
<font size="2">@Password [nvarchar]<font size="2" color="#808080">(</font><font size="2">50</font><font size="2" color="#808080">),</font><br />
</font><font size="2">@PasswordHash [nvarchar]</font><font size="2" color="#808080">(</font><font size="2">50</font><font size="2" color="#808080">)</font><font size="2"> </font><font size="2" color="#0000ff">OUTPUT</font><br />
<font size="2" color="#0000ff">WITH</font><font size="2"> </font><font size="2" color="#0000ff">EXECUTE</font><font size="2"> </font><font size="2" color="#0000ff">AS</font><font size="2"> </font><font size="2" color="#0000ff">CALLER</font><br />
<font size="2" color="#0000ff">AS<br />
EXTERNAL<font size="2"> NAME [Rapid.Database.Security]</font><font size="2" color="#808080">.</font><font size="2">[Rapid.Database.Security.User]</font><font size="2" color="#808080">.</font><font size="2">[HashPassword]</font><br />
</font><font size="2" color="#0000ff">GO<br />
</font><br />
The three part name of the C# method are:</p>
<ol>
<li>Assembly name which SQL Server knows about (basically the name of the DLL without the file extension).</li>
<li>The full name to the class, in our case the namespace then the class.  Our namespace is Rapid.Database.Security with the name of the class after that.</li>
<li>The name of the method within the C# class.</li>
</ol>
<p>Don&#8217;t forget to enable CLR within SQL Server using the sp_configure before trying to use the CLR procedure.</p>
<p> At this point I am able to run the procedure and pass in text and get back the hash.</p>
<p><font size="2" color="#0000ff">declare<font size="2"> @PasswordHash </font><font size="2" color="#0000ff">varchar</font><font size="2" color="#808080">(</font><font size="2">50</font><font size="2" color="#808080">)</font><br />
</font><font size="2" color="#0000ff">exec</font><font size="2"> HashPassword</font><font size="2" color="#0000ff"> </font><font size="2">@password</font><font size="2" color="#808080">=</font><font size="2" color="#ff0000">&#8216;test&#8217;</font><font size="2" color="#808080">,</font><font size="2"> @passwordHash</font><font size="2" color="#808080">=</font><font size="2">@PasswordHash </font><font size="2" color="#0000ff">OUTPUT</font><br />
<font size="2" color="#0000ff">SELECT<font size="2"> @PasswordHash</font></font></p>
<p>The value of this is: A94A8FE5CCB19BA61C4C0873D391E987982FBBD3</p>
<p>I hope that you find this useful. It took me and a .NET developer a couple of hours to get all this working correctly and loaded into the SQL Server correctly. I know that I&#8217;ll be referring back to this if I ever need to attach a CLR method into SQL again.</p>
<p>Do keep in mind that when we were doing this, we created this as a separate DLL for the SQL Server as to get the original DLL into the SQL Server, SQL wanted us to load up a bunch of other assemblies that some of the other methods required.</p>
<p>Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/my-experience-with-putting-a-crl-procedure-into-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
