How to call C# function in stored procedure

一曲冷凌霜 提交于 2020-01-29 05:12:25

问题


SQL Server 2005 supports CLR so it means we can use CLR in backend so how to do that, I have some function in c# which do some complex manipulation with date-time variable now I want to use those functions in SP. First of all IS IT POSSIBLE TO DO THIS.


回答1:


Yes, it is possible to use .NET in a SQL Server 2005 database. Be aware that the .NET version supported by SQL Server 2005 is 2.0.

Here's a link for an introduction to Making a CLR stored procedure using Visual Studio




回答2:


Take a look this TSQL example

USE [XXX] GO SET ANSI_NULLS OFF GO SET QUOTED_IDENTIFIER OFF GO CREATE PROCEDURE [dbo].[Testing_XXX]
@broadcastId [int],
@XXXTemplateHtml [nvarchar](max),
@XXXTemplateText [nvarchar](max),
@XXXTemplateSubject [nvarchar](max),
@XXXTemplateEmailHeaders [nvarchar](max),
@XXXTemplateHeader [nvarchar](max),
@XXXTemplateFooter [nvarchar](max),
@masterTemplate [nvarchar](max),
@parseOptions [nvarchar](4000),
@xsltTemplate [nvarchar](max) OUTPUT WITH EXECUTE AS CALLER AS EXTERNAL NAME  SolutionXXX.ProjectXXX].[StoredProcedures].[XXX_Parser_Parse] GO

When you call

EXTERNAL NAME [SolutionXXX.ProjectXXX].[StoredProcedures].[XXX_Parser_Parse]

It invokes a C# function looks like this

 [SqlProcedure]
public static void XXX_Parser_Parse(
    SqlInt32 broadcastId,
    [SqlFacet(MaxSize = -1)] 
    SqlString XXXTemplateHtml,
    [SqlFacet(MaxSize = -1)] 
    SqlString XXXTemplateText,
    [SqlFacet(MaxSize = -1)] 
    SqlString XXXTemplateSubject,
    [SqlFacet(MaxSize = -1)] 
    SqlString XXXTemplateEmailHeaders,
    [SqlFacet(MaxSize = -1)] 
    SqlString XXXTemplateHeader,
    [SqlFacet(MaxSize = -1)] 
    SqlString XXXTemplateFooter,
    [SqlFacet(MaxSize = -1)] 
    SqlString masterTemplate,
    SqlString parseOptions,
    [SqlFacet(MaxSize = -1)] 
    out SqlString xsltTemplate)
{
//blah blah blh
}


来源:https://stackoverflow.com/questions/4002382/how-to-call-c-sharp-function-in-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!