1 create a CLR User-defined function.
(1) create a Database -- SQL Server Project; Add a new item(User-defined function); Wirte a helloword function
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString fnHelloWorld()
{
// Put your code here
return new SqlString("Hello World");
}
};
(2) build the project. get the relative assembly(*.dll)
(3) Open the SSMS. Select one database, expand the menu, get the "Assemblies" node, right click, "New assembly", import the previoud assembly. set the other options if possible.
(4) Create one SQL Function, which call the udf in the assembly
CREATE FUNCTION fnHelloWorld()
RETURNS NVARCHAR(50) WITH EXECUTE AS CALLER
AS EXTERNAL NAME CLRFunction.UserDefinedFunctions.fnHelloWorld
(5) Use the created function
select dbo.fnHelloWorld()
创建CLR用户定义函数
本文详细介绍了如何在SQL Server中创建CLR(Common Language Runtime)用户定义函数,包括创建数据库项目、编写C#代码实现Hello World函数、构建项目并导入DLL到SQL Server、创建外部调用的SQL函数以及使用该函数的过程。
910

被折叠的 条评论
为什么被折叠?



