 




<?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 Server stored procedures</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/sql-server/tag/sql-server-stored-procedures/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/sql-server</link>
	<description></description>
	<lastBuildDate>Fri, 24 May 2013 17:04:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Encrypting data in the same column</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/encrypting-data-in-the-same-column/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/encrypting-data-in-the-same-column/#comments</comments>
		<pubDate>Wed, 10 Oct 2012 09:00:00 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[Data Encryption]]></category>
		<category><![CDATA[Data Loss]]></category>
		<category><![CDATA[Data Security]]></category>
		<category><![CDATA[Data Types]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[Database Design]]></category>
		<category><![CDATA[Database security]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Identity theft]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server stored procedures]]></category>
		<category><![CDATA[Stored Procedures]]></category>
		<category><![CDATA[T/SQL]]></category>
		<category><![CDATA[Tables]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/?p=2175</guid>
		<description><![CDATA[I wrote a little while ago about the fact that sensitive data needs to be encrypted within the database for all applications.  This is the first technique that is available to you to encrypt data in a database with as little outage as possible. In this technique we’ll encrypt the data using just a single [...]]]></description>
				<content:encoded><![CDATA[<p>I wrote a little while ago about the fact that sensitive <a href="http://itknowledgeexchange.techtarget.com/sql-server/sensitive-data-must-be-encrypted/">data needs to be encrypted</a> within the database for all applications.  This is the first technique that is available to you to encrypt data in a database with as little outage as possible.</p>
<p>In this technique we’ll encrypt the data using just a single column.  This technique requires butting some additional logic within the application to figure out if the value is encrypted or not, but other than that logic, which you can leave in and strip out later the changes to the application are pretty minimal as the column stays the same, so that means that the stored procedures don’t need to be changed.</p>
<p>The first thing to remember is that the encrypted data will be larger, possibly much larger than the plain text version of the data.  Because of this you’ll need to increase the size of the field which you’ll be putting the data into.  Now the good news is that if this column isn’t indexed this change should be pretty quick and easy as it should just be a meta change which tells the SQL Server that the column size can be bigger without having to actually change the pages.  You can see this by making some changes to the [HumanResources].[Employee] table within the AdventureWorks database.  By turning on STATISTICS IO and using the ALTER TABLE statement we see that there is no IO generated when we change the size of the LoginID column from nvarchar(256) to nvarchar(512).</p>
<blockquote><p>set statistics io on<br />
alter table MyTable<br />
alter column LoginID nvarchar(512)</p></blockquote>
<p>Once the column is made larger the .NET code needs to be modified to see if the data is compressed for not.  Now there is no sure fire way to check to see if a value has been encrypted or not, but a pretty good test is to look at the last two characters of the value.  If they are both an equal sign (==) then it is probably safe to assume that the value is encrypted.  To don’t want to just attempt to decrypt the data and look for an error message, and if there is an error assume that the encrypted value is in plain text, throwing and catching error messages in .NET is very expensive, especially compared to simply checking to see if the last two characters are an equal sign.  This isn’t to say that you shouldn’t have TRY/CATCH logic around the code that decrypts the values as someone could easily enough put two equal signs at the end of their password.</p>
<p>At this point either a .NET app or a T-SQL script can loop through the values in the table which aren’t encrypted and then encrypt them, updating the rows which aren’t already encrypted.</p>
<p>Look for more blog posts in this series on how to encrypt data which already exists within your applications database.</p>
<p>Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/encrypting-data-in-the-same-column/feed/</wfw:commentRss>
		<slash:comments>2</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>
		<item>
		<title>Back To Basics: Stored Procedures, the work horse of the database</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/back-to-basics-stored-procedures-the-work-horse-of-the-database/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/back-to-basics-stored-procedures-the-work-horse-of-the-database/#comments</comments>
		<pubDate>Thu, 10 Apr 2008 20:00:17 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[Back To Basics]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server stored procedures]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/back-to-basics-stored-procedures-the-work-horse-of-the-database/</guid>
		<description><![CDATA[Stored procedures are extremely useful objects.  Not only do they store T/SQL scripts for later execution, but they also provide us with an extremely important security barrier between the user interface and the database.  The security barrier is used to prevent the users from needing SELECT, INSERT, UPDATE and/or DELETE rights directly to the database tables and views.]]></description>
				<content:encoded><![CDATA[<p>Stored procedures are extremely useful objects.  Not only do they store T/SQL scripts for later execution, but they also provide us with an extremely important security barrier between the user interface and the database.  The security barrier is used to prevent the users from needing SELECT, INSERT, UPDATE and/or DELETE rights directly to the database tables and views. </p>
<p>This is done through what is called permissions chaining.  When a user has rights to execute a stored procedure they are given temporary rights to use the table objects within the procedures which are used by the table.</p>
<p>Creating stored procedures is very easy.  Take your Transact SQL code and put it below the CREATE PROCEDURE command, and end the batch.  Like all other database objects the name of the stored procedure must be unique within the schema (or owner for SQL 2000 and below).  As an example lets create a stored procedure which returns the names of all the tables in the current database.</p>
<p><code>CREATE PROCEDURE ShowTables AS<br />
SELECT schema_name(schema_id), name<br />
FROM sys.tables<br />
GO</code></p>
<p>As you can see the basic syntax is very simple.  To run this stored procedure we simply run the stored procedure name.</p>
<p><code>exec ShowTables</code></p>
<p>You can add in input parameters to handle filtering, or which would need to be inserted into a table.  An input parameter is simply a variable which you set when you run the procedure.  You can access the value of the input parameter within the stored procedure as you would any other variable.  Let&#8217;s look at the same procedure but this time we want to filter the tables by the first letter.</p>
<p><code>CREATE PROCEDURE ShowTables<br />
  @FilterChar NVARCHAR(2)<br />
AS<br />
SET @FilterChar = @FilterChar + '%'<br />
SELECT schema_name(schema_id), name<br />
FROM sys.tables<br />
WHERE name LIKE @FilterChar<br />
GO</code></p>
<p>In this example as you can see we take the input parameter, and add the % wild card, then use the variable to filter down the records to see only the records which start with the character we supply.  Running the stored procedure with an input parameter is just as easy.</p>
<p><code>exec ShowTables @FilterChar=N'C'</code></p>
<p>We can also use output parameters to get values back from the stored procedures.  Output parameters are used basically in the same way that input parameters are, however you add the OUTPUT keyword after the parameter.  Within the stored procedure simply set the output variable to the value you want it to return to the calling code.  This can be done anywhere within the stored procedure, as long as the variable still holds the value when the stored procedure has completed it&#8217;s execution the value will be returned to the calling code.  First lets look at the code to create the stored procedure.</p>
<p> <font face="Courier New">CREATE PROCEDURE ShowTables<br />
  @FilterChar NVARCHAR(2),<br />
</font><font face="Courier New">  @RowCount INT OUTPUT<br />
AS<br />
SET @FilterChar = @FilterChar + &#8216;%&#8217;<br />
SELECT schema_name(schema_id), name<br />
FROM sys.tables<br />
WHERE name LIKE @FilterChar</font></p>
<p><font face="Courier New">SET @RowCount = @@ROWCOUNT<br />
GO</font></p>
<p>We run the stored procedure in much the same way we do with the input parameter.</p>
<p><font face="Courier New">DECLARE @RowCount INT<br />
exec ShowTables @FilterChar=N&#8217;C', @RowCount=@RowCount OUTPUT<br />
SELECT @RowCount</font> </p>
<p>In this case we are simply returning the row count as a second record set, but you&#8217;ll get the basic idea.</p>
<p>As I wrote earlier you can add records to a table with the stored procedure.</p>
<p><code>CREATE PROCEDURE InsertTable<br />
  @Id INT,<br />
  @Value VARCHAR(20)<br />
AS</code><code>INSERT INTO SomeTable<br />
(Id, Value)<br />
VALUE<br />
(@Id, @Value)<br />
GO</p>
<p></code>As you can see it&#8217;s a very basic method.  It&#8217;s a regular insert statement with the parameters passed to it.</p>
<p>I know that this was a bit longer than the other posts, but I hope that you found it worth while.</p>
<p>Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/back-to-basics-stored-procedures-the-work-horse-of-the-database/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
