{"id":273,"date":"2011-05-15T04:15:20","date_gmt":"2011-05-14T23:15:20","guid":{"rendered":"http:\/\/kennywu.info\/?p=273"},"modified":"2011-05-15T20:29:08","modified_gmt":"2011-05-15T15:29:08","slug":"clr-in-sql-server","status":"publish","type":"post","link":"http:\/\/kennywu.info\/?p=273","title":{"rendered":"CLR in SQL Server"},"content":{"rendered":"<p>According to Microsoft, Use Transact-SQL when the code will mostly perform data access with little or no procedural logic. Use managed code for CPU-intensive functions and procedures that feature complex logic, or when you want to make use of the BCL of the .NET Framework.<\/p>\n<p><strong>1. Create SQL Stored Procedure in visual stuido<\/strong><br \/>\nProject name: &#8220;FirstSqlClass.cs&#8221;; class name:&#8221;FirstSqlClr&#8221; and function name:&#8221;ListEmployees&#8221; <\/p>\n<pre lang='csharp'>\r\nusing System;\r\nusing System.Data;\r\nusing System.Data.SqlClient;\r\nusing System.Data.SqlTypes;\r\nusing Microsoft.SqlServer.Server;   \r\n\r\n\r\npublic partial class FirstSqlClr\r\n{\r\n    [Microsoft.SqlServer.Server.SqlProcedure]\r\n\r\n    public static void ListEmployees(SqlString fLetter, SqlString orderBy, out SqlString outputParameter)\r\n    {\r\n        string sqlCmd = \"\";\r\n        sqlCmd += \" select c.LastName, c.FirstName, c.EmailAddress \";\r\n        sqlCmd += \" from Person.Contact as c inner join HumanResources.Employee as e on c.ContactID = e.ContactID \";\r\n        sqlCmd += \" where left(c.LastName, 1) = @fLetter\";\r\n        sqlCmd += \" order by \" + orderBy.ToString();        \r\n        \r\n        SqlContext.Pipe.Send(\"Stored Procedure started at: \" + DateTime.Now.ToString() + \"\\n\");\r\n        using (SqlConnection conn = new SqlConnection(\"context connection = true\"))\r\n        {\r\n            conn.Open();\r\n            SqlCommand cmd = new SqlCommand(sqlCmd, conn);\r\n            cmd.Parameters.Add(new SqlParameter (\"@fLetter\", SqlDbType.NVarChar, 1));\r\n            cmd.Parameters[0].Value = fLetter;\r\n            \r\n            SqlDataReader rdr = cmd.ExecuteReader();\r\n            SqlContext.Pipe.Send(rdr);\r\n        }\r\n        outputParameter = \"after some manipulation that has to be done by a procedure language.\";\r\n        SqlContext.Pipe.Send(\"Stored Procedure ended at: \" + DateTime.Now.ToString() + \"\\n\");\r\n    }    \r\n};\r\n<\/pre>\n<p><strong>2. Set Target Framework to &#8220;.Net 2.0&#8221;<\/strong> SQL 2008 only support .Net 2.0. Comply to DLL.<br \/>\n<strong>3. Enable Clr in Sql Server:<\/strong><br \/>\n<code><br \/>\nsp_configure 'clr enabled', 1<br \/>\ngo<br \/>\nreconfigure<br \/>\ngo<br \/>\n<\/code><br \/>\n<strong>4. Register the assembly in SQL Server<\/strong> Locate the Dll file and set Permission to &#8220;SAFE&#8221;.<br \/>\n<code><br \/>\nuse AdventureWorks<br \/>\ngo<br \/>\ncreate assembly FirstSqlClrClass from<br \/>\n'C:\\Users\\kwu\\Documents\\visual studio 2010\\projects\\FirstSqlClrClass\\FirstSqlClrClass\\bin\\Debug\\FirstSqlClrClass.dll'<br \/>\ngo<br \/>\n<\/code><br \/>\n<strong>5. Create Stored Procedure<\/strong><br \/>\n<code><br \/>\nuse AdventureWorks<br \/>\ngo<br \/>\ncreate procedure FirstListEmployees(@fLetter Nvarchar(1), @OrderBy NVarchar(20), @outputParameter NVarchar(100) OUT) as<br \/>\nexternal name FirstSqlClrClass.FirstSqlClr.ListEmployees<br \/>\ngo<br \/>\n<\/code><br \/>\nThe the SP can be used as regular T-Sql stored procedure.<br \/>\n<code><br \/>\nuse AdventureWorks<br \/>\ngo<br \/>\nDECLARE\t@return_value int,<br \/>\n\t\t@outputParameter nvarchar(100)<\/p>\n<p>EXEC\t@return_value = [dbo].[FirstListEmployees]<br \/>\n\t\t@fLetter = 'B',<br \/>\n\t\t@OrderBy = 'EmailAddress',<br \/>\n\t\t@outputParameter = @outputParameter OUTPUT<\/p>\n<p>SELECT\t@outputParameter as N'@outputParameter'<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>According to Microsoft, Use Transact-SQL when the code will mostly perform data access with little or no procedural logic. Use managed code for CPU-intensive functions and procedures that feature complex logic, or when you want to make use of the &hellip; <a href=\"http:\/\/kennywu.info\/?p=273\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[69],"class_list":["post-273","post","type-post","status-publish","format-standard","hentry","category-database","tag-clr"],"_links":{"self":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=273"}],"version-history":[{"count":11,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/273\/revisions"}],"predecessor-version":[{"id":280,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/273\/revisions\/280"}],"wp:attachment":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}