Click here to Skip to main content
13,589,266 members
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 15 Oct 2006

Deploy and Debug CLR store procedure

, 15 Oct 2006
Rate this:
Please Sign up or sign in to vote.
This article will drive you, how deploy and debug CLR store procedure
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->


Hi All,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p>


Here is some cool stuff related to Deploy and Debugging of CLR Store Procedure, I will cover here how to Deploy and Debugging CLR store procedure using VS 2005.<o:p>

I suggest before reading this article please read my previous articles named “CLR store procedure vs. T-SQL store procedure” and “How to create CLR store procedure”. <o:p>

1.      While deploying CLR store procedure you must have “CREATE ASSEMBLY” permission. Once deployed on SQL Server you can see and execute CLR store procedure there and then only as sown below.

Sample screenshot


2.      You might observe “Test Script” folder while creating CLR store procedure in VS 2005. This folder contains “Test.sql” file, if you open and see that file it consist different section each for specific task like execution of store procedure, user defined function etc. This script file is purely made for debugging purpose. Please follow give steps to debug CLR store procedure.  <o:p>

Steps for debugging CLR store procedure,<o:p>

a.      Open “Test.sql” file and insert following code at the end of file. <o:p>

EXEC dbo.GetRoles <o:p>

--Created in previous article named “How to create CLR store procedure”  <o:p>

EXEC dbo.PrintDate 'This will go into txt file'<o:p>

b.      “PrintDate” store procedure access external resources like file system. This store procedure is writing to a file. While accessing external resources, need “TRUSTWORTHY” permission.<o:p>

c.      Fire following command to achieve “TRUSTWORTHY” permission.<o:p>


d.      What follows is code for “PrintDate” CLR store procedure.<o:p>


       public static void PrintDate(string name)<o:p>



            SqlPipe p;<o:p>

            p = SqlContext.Pipe;<o:p>

FileIOPermission filePerm = new FileIOPermission(FileIOPermissionAccess.AllAccess, @"C:\TestFile.txt");<o:p>


            StreamWriter sw = new StreamWriter(@"C:\TestFile.txt");<o:p>



            sw.Close(); <o:p>


using (SqlConnection connection = new <o:p>


SqlConnection("context connection=true"))<o:p>



SqlCommand command = new SqlCommand("SELECT * FROM     Department", connection);<o:p>

            SqlDataReader reader = command.ExecuteReader();<o:p>






e.      Since “PrintDate” CLR store procedure access external resources as discussed above, we need to incorporate this change in VS 2005 before deploying CLR store procedure as shown below. Following screen shows setting of “Permission Level” as “External”.  <o:p>


<o:p>Sample screenshot 

f.       Now I assume you have successfully deployed CLR store procedure and incorporated respective execute command in “Test.sql”.<o:p>

g.      Mark break point in “PrintDate” function and Run the application in VS 2005. Application halt at break point like any .NET application. <o:p>

Sample screenshot    <o:p>

h.      After execution complete you can observe “TestFile.txt” get generated with text inside passed as input parameter to CLR store procedure, check out “Output” window which contains results for “GetRole” CLR store procedure and SQL statement of “PrintDate” function "SELECT * FROM     Department" as shown below.<o:p>


<o:p>Sample screenshot 

3.      To enable CLR on SQL Server please fire following query, on particular database.<o:p>

EXEC sp_configure 'show advanced options' , '1';<o:p>




EXEC sp_configure 'clr enabled' , '1'<o:p>



-- Turn advanced options back off<o:p>

EXEC sp_configure 'show advanced options' , '1';<o:p>

go <o:p>



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Malpani Amol
Web Developer
India India
I am a .NET developer cum designer. I work for Zensar Technologies Ltd. Pune (INDIA), I consult, I develop, I debug and besides that I speak at conferences and user groups and I also write. I have written some articles on WCF, WF, SSIS, Compression decompression, CLR Store Procedure, SQL Server Reporting Services 2005, Extended grid and lot more to count.
Reach to me

You may also be interested in...


Comments and Discussions

GeneralDeploy and Debug CLR User Defined Functions Pin
franklinraj12-Nov-09 7:06
memberfranklinraj12-Nov-09 7:06 
GeneralThanks Pin
sashidhar30-Oct-09 19:22
membersashidhar30-Oct-09 19:22 
GeneralExcellent article Pin
Kaila.Kamlesh9-Sep-07 21:58
memberKaila.Kamlesh9-Sep-07 21:58 
GeneralThanx for sharing Pin
salman_mubbashir8-Dec-06 21:48
membersalman_mubbashir8-Dec-06 21:48 
GeneralNon-VS deployment Pin
Mark Nischalke15-Oct-06 5:31
memberMark Nischalke15-Oct-06 5:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.180615.1 | Last Updated 15 Oct 2006
Article Copyright 2006 by Malpani Amol
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid