日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

SQL Server 2000 中使用正則表達式_Mssql數(shù)據(jù)庫教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:數(shù)據(jù)庫的分離及附加
1、分離數(shù)據(jù)庫 exec sp_detach_db dataname --eg: dataname:要分離的數(shù)據(jù)庫名 2、附加數(shù)據(jù)庫 1 exec sp_attach_single_file_db newdataname,mdfPath /*eg: newdataname:要附加上的新數(shù)據(jù)庫名稱(可以是原來的) mdfPath='C:\Program Files\Microsoft SQL Serve

這兩天有個需求,需要在數(shù)據(jù)庫中判斷字符串的格式,于是從網(wǎng)上搜集了一些資料,整理了一下。

下面這個是一個自定義函數(shù),用戶可以調用這個函數(shù)判斷指定的字符串是否符合正則表達式的規(guī)則.
CREATE FUNCTION dbo.find_regular_expression
(
@source varchar(5000), --需要匹配的源字符串
@regexp varchar(1000), --正則表達式
@ignorecase bit = 0 --是否區(qū)分大小寫,默認為false
)
RETURNS bit --返回結果0-false,1-true
AS
BEGIN

--0(成功)或非零數(shù)字(失�。�,是由 OLE 自動化對象返回的 HRESULT 的整數(shù)值。
DECLARE @hr integer

--用于保存返回的對象令牌,以便之后對該對象進行操作
DECLARE @objRegExp integer DECLARE @objMatches integer

--保存結果
DECLARE @results bit

/*
創(chuàng)建 OLE 對象實例,只有 sysadmin 固定服務器角色的成員才能執(zhí)行 sp_OACreate,并確定機器中有VBScript.RegExp類庫
*/
EXEC @hr = sp_OACreate 'VBScript.RegExp', @objRegExp OUTPUT
IF @hr <> 0 BEGIN
SET @results = 0
RETURN @results
END
/*
以下三個分別是設置新建對象的三個屬性。下面是'VBScript.RegExp'中常用的屬性舉例:
Dim regEx,Match,Matches '建立變量。
Set regEx = New RegExp '建立一般表達式。
regEx.Pattern= patrn '設置模式。
regEx.IgnoreCase = True '設置是否區(qū)分大小寫。
regEx.Global=True '設置全局可用性。
set Matches=regEx.Execute(string) '重復匹配集合
RegExpTest = regEx.Execute(strng) '執(zhí)行搜索。
for each match in matches '重復匹配集合
RetStr=RetStr &"Match found at position "
RetStr=RetStr&Match.FirstIndex&".Match Value is '"
RetStr=RetStr&Match.Value&"'."&vbCRLF Next
RegExpTest=RetStr

*/
EXEC @hr = sp_OASetProperty @objRegExp, 'Pattern', @regexp
IF @hr <> 0 BEGIN
SET @results = 0
RETURN @results
END
EXEC @hr = sp_OASetProperty @objRegExp, 'Global', false
IF @hr <> 0 BEGIN
SET @results = 0
RETURN @results
END
EXEC @hr = sp_OASetProperty @objRegExp, 'IgnoreCase', @ignorecase
IF @hr <> 0 BEGIN
SET @results = 0
RETURN @results
END
--調用對象方法
EXEC @hr = sp_OAMethod @objRegExp, 'Test', @results OUTPUT, @source
IF @hr <> 0 BEGIN
SET @results = 0
RETURN @results
END
--釋放已創(chuàng)建的 OLE 對象
EXEC @hr = sp_OADestroy @objRegExp
IF @hr <> 0 BEGIN
SET @results = 0
RETURN @results
END
RETURN @results
END
下面是一個簡單的測試sql語句,可以直接在查詢分析器中運行。
DECLARE @intLength AS INTEGER
DECLARE @vchRegularExpression AS VARCHAR(50)
DECLARE @vchSourceString as VARCHAR(50)
DECLARE @vchSourceString2 as VARCHAR(50)
DECLARE @bitHasNoSpecialCharacters as BIT

-- 初始化變量
SET @vchSourceString = 'Test one This is a test!!'
SET @vchSourceString2 = 'Test two This is a test'

-- 我們的正則表達式應該類似于
-- [a-zA-Z ]{}
-- 如: [a-zA-Z ]{10} ... 一個十字符的字符串

-- 獲得字符串長度
SET @intLength = LEN(@vchSourceString)

-- 設置完整的正則表達式
SET @vchRegularExpression = '[a-zA-Z ]{' + CAST(@intLength as varchar) + '}'

-- 是否有任何特殊字符
SET @bitHasNoSpecialCharacters = dbo.find_regular_expression(@vchSourceString, @vchRegularExpression,0)

PRINT @vchSourceString
IF @bitHasNoSpecialCharacters = 1 BEGIN
PRINT 'No special characters.'
END ELSE BEGIN
PRINT 'Special characters found.'
END

PRINT '**************'

-- 獲得字符串長度
SET @intLength = LEN(@vchSourceString2)

-- 設置完整的正則表達式
SET @vchRegularExpression = '[a-zA-Z ]{' + CAST(@intLength as varchar) + '}'

-- 是否有任何特殊字符
SET @bitHasNoSpecialCharacters = dbo.find_regular_expression(@vchSourceString2, @vchRegularExpression,0)

PRINT @vchSourceString2
IF @bitHasNoSpecialCharacters = 1 BEGIN
PRINT 'No special characters.'
END ELSE BEGIN
PRINT 'Special characters found.'
END

GO

來源:lizhiwen的cnblogs

分享:查詢表里有N組相同記錄的SQL語句
表里面有N條相同的記錄,要查出來. 如: ID Name 12 d 34 e 543 t 34 e 12 d 45 y 543 t 查詢的結果應為: ID Name 12 d 12 d 34 e 34 e 543 t 543 t 假如表名為Table03,可以用下面語句輕松實現(xiàn): select id,name from table03 where name in(select name f

來源:模板無憂//所屬分類:Mssql數(shù)據(jù)庫教程/更新時間:2012-06-10
相關Mssql數(shù)據(jù)庫教程

Mssql數(shù)據(jù)庫教程Rss訂閱編程教程搜索