.NET Managed Code Assemblies Microsoft SQL Server 2005 and higher provide SQLCLR, a variety of the CLR runtime that permits .NET managed code to be run on the database itself. The SQLCLR is wholly managed by the SQL Server database engine. You can think of the SQLCLR as a specialized version of the .NET CLR that is hosted directly within the Microsoft SQL Server 2005 or Microsoft SQL Server 2008 database. SQLCLR assemblies are especially valuable when large amounts of data are not required as the result of an operation, for example, when large amounts of redundant data would have had to be transferred between the client and server. It is important to recognize that SQLCLR does not replace TSQL, the traditional means to write stored procedures. In order for a SQLCLR assembly to access data, it uses ADO.NET to access data and to execute SQL statements. Consider the following sample SQLCLR assembly, which is compiled and deployed on a Microsoft SQL Server 2005 instance. [Microsoft.SqlServer.Server.SqlFunction(Name = "MoonPhase")] public static double MoonPhase(DateTime inDate) { // BACKGROUND: On average, the Moon goes through its // phases in 29.53 days (i.e., New Moon to New Moon). // I stress "average" because orbital mechanics makes // this shorter for some months, and longer for others. // May 26, 1979 is a good Epoch because the New Moon // began at exactly 12:00 AM UTC // Subtract the Epoch from the DateTime passed in // (assumed UTC) TimeSpan sinceEpoch = inDate - new DateTime(1979, 5, 26, 0, 0, 0, 0, DateTimeKind.Utc); // Calculate and return the percentage of the current phase return (sinceEpoch.TotalDays % 29.53D) / 29.53D; } This assembly is accessed in the same way as any scalar function, using a SQL select statement such as SELECT OrderDate, SubTotal, dbo.MoonPhase(OrderDate) AS PctOfCycle FROM Sales.SalesOrderHeader Should the Managed Assembly Stored Procedure want to return a stream of data, a separate mechanism can be used to return this data. In this instance, the SqlPipe.Send() and associated overloads can be used. In certain cases, the DataReader may be used to marshall the return data. For more information about using SQLCLR, refer to "Managed Data Access Inside SQL Server with ADO.NET and SQLCLR" at http://msdn2.microsoft.com/en-us/library/ms345135.aspx.