CLR用戶定義函數(shù)只是在.NET 程序集中定義的靜態(tài)方法。CREATE FUNCTION 語句已擴(kuò)展為支持創(chuàng)建 CLR 用戶定義函數(shù)。
1、創(chuàng)建數(shù)據(jù)庫項(xiàng)目

2、添加用戶定義函數(shù)

以下是演示代碼:
Code using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System.Text.RegularExpressions;
// 示意代碼 public partial class UserDefinedFunctions { public static readonly RegexOptions Options = RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline;
[Microsoft.SqlServer.Server.SqlFunction] public static string RegexValue(SqlChars input, SqlString pattern) { Regex regex = new Regex(pattern.Value, Options);
return regex.Match(new string(input.Value)).Value; } }
|
3、將自定義函數(shù)關(guān)聯(lián)到數(shù)據(jù)庫

4、Sql 查詢分析器

為了確保SQL可以執(zhí)行托管代碼,執(zhí)行下面的語句:
EXEC sp_configure 'clr enabled', 1
sql 如下: select dbo.RegexValue('2008-09-02',N'\d{4}') from Table
=============================================
|
【編輯推薦】
- 深入淺出MySQL雙向復(fù)制技術(shù)
- 并行查詢讓SQL Server加速運(yùn)行
- SQL Server 2005常見問題淺析