SQL.CLR

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

ContractedBlock.gifExpandedBlockStart.gifUdf helloworld
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

ContractedBlock.gifExpandedBlockStart.gifSql function
CREATE FUNCTION fnHelloWorld()
RETURNS NVARCHAR(50WITH EXECUTE AS CALLER
AS EXTERNAL NAME CLRFunction.UserDefinedFunctions.fnHelloWorld

 

(5) Use the created function

ContractedBlock.gifExpandedBlockStart.gifcall the udf
select dbo.fnHelloWorld()
发布了146 篇原创文章 · 获赞 35 · 访问量 27万+

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览