最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

T-SQL中使用正則表達(dá)式函數(shù)

 更新時(shí)間:2010年06月29日 15:46:35   作者:  
有想過(guò)在T-Sql使用正則表達(dá)式嗎?是的,完全可以的,我們可以用SQL SERVER CLR sql function來(lái)實(shí)現(xiàn)這一功能。
首先,我們?cè)赩STS中創(chuàng)建一Database Project,增一個(gè)class, 實(shí)現(xiàn)下面的一個(gè)方法:
復(fù)制代碼 代碼如下:

/// <summary>
/// Regs the ex match.
/// </summary>
/// <param name="inputValue">The input value.</param>
/// <param name="regexPattern">The regex pattern.</param>
/// <remarks>Author: Petter Liu http://wintersun.cnblogs.com </remarks>
/// <returns>1 match,0 not match</returns>
[SqlFunction]
public static bool RegExMatch(string inputValue, string regexPattern)
{
// Any nulls - we can't match, return false
if (string.IsNullOrEmpty(inputValue) || string.IsNullOrEmpty(regexPattern))
return false;

Regex r1 = new Regex(regexPattern.TrimEnd(null));
return r1.Match(inputValue.TrimEnd(null)).Success;
}

好了,Build后Deploy到你的Target database就OK了,VisualStudio會(huì)自動(dòng)注冊(cè)這個(gè)程序集的。如果,你想手動(dòng)注冊(cè)程序集,可執(zhí)行以下的T-SQL:
復(fù)制代碼 代碼如下:

CREATE ASSEMBLY [RegExCLR] FROM 'RegExCLR.dll';

-- Add the REGEX function. We want a friendly name
-- RegExMatch rather than the full namespace name.
-- Note the way we have to specify the Assembly.Namespace.Class.Function
-- NOTE the RegExCLR.RegExCLR
-- (one is the assembly the other is the namespace)
CREATE FUNCTION RegExMatch ( @inputCalue NVARCHAR(4000),
@regexPattern NVARCHAR(4000) ) RETURNS BIT
AS EXTERNAL NAME RegExCLR.RegExCLR.ClrClass.RegExMatch;

OK, 一切OK的后,我們來(lái)測(cè)試下:

select COUNT(1) from Threads where dbo.RegExMatch(ThreadId,'^[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\)|}]?$')=1
上面的T-SQL是找出Threads表ThreadId是GUID的記錄數(shù)。 等于1是匹配,^[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\)|}]?$ 匹配GUID的正則表達(dá)式。

完了,希望這篇POST對(duì)您有幫助。

您可能對(duì)以下POST感興趣:

SQLSERVER2008中CTE的Split與CLR的性能比較

SQLSERVER使用CLR Stored Procedure導(dǎo)出數(shù)據(jù)到Excel

相關(guān)文章

最新評(píng)論

荔浦县| 遂昌县| 永胜县| 来凤县| 锡林浩特市| 灵台县| 延吉市| 常宁市| 大宁县| 灵石县| 北安市| 龙井市| 南乐县| 衡东县| 津市市| 驻马店市| 涞水县| 澜沧| 武乡县| 凉城县| 宝清县| 松阳县| 寿阳县| 察隅县| 云安县| 白水县| 祥云县| 即墨市| 新乡县| 罗城| 长顺县| 靖安县| 尚义县| 青川县| 安丘市| 襄垣县| 辰溪县| 历史| 定陶县| 罗定市| 东乌珠穆沁旗|