CLR用戶定義函數只是在.NET 程序集中定義的靜態方法。CREATE FUNCTION 語句已擴展為支持創建 CLR 用戶定義函數。
1、創建數據庫項目

2、添加用戶定義函數

以下是演示代碼:
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、將自定義函數關聯到數據庫

4、Sql 查詢分析器

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