using System; using System.Text.RegularExpressions; using Microsoft.SqlServer.Server; public partial class UserDefinedFunctions { [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true,IsPrecise = true)] public static bool RegExIsMatch(string pattern,string matchString) { // 在此处放置代码 Regex reg = new Regex(pattern.TrimEnd(null)); return reg.Match(matchString.TrimEnd(null)).Success; } }; use AdventureWorks GO CREATE ASSEMBLY UDF FROM 'C:/Users/Roy/Documents/Visual Studio 2008/Projects/UDF/UDF/bin/Release/UDF.dll' CREATE FUNCTION fCLRExample ( @Pattern nvarchar(max), @MatchString nvarchar(max) ) RETURNS BIT AS EXTERNAL NAME UDF.UserDefinedFunctions.RegExIsMatch SELECT ContactID,FirstName,LastName,EmailAddress,Phone FROM Person.Contact WHERE DBO.fCLRExample('[a-zA-Z0-9_/-]+@([a-zA-Z0-9_/-]+/.)+(com|org|edu|mil|net|cn)',EmailAddress)=1