create function dbo.RegexMatch (
@pattern varchar(2000),
@matchstring varchar(8000)
)
returns int
as
begin
declare @objRegexExp int
declare @strErrorMessage varchar(255)
declare @hr int,@match bit
exec @hr= sp_OACreate 'VBScript.RegExp', @objRegexExp out
if @hr = 0
exec @hr= sp_OASetProperty @objRegexExp, 'Pattern', @pattern
if @hr = 0
exec @hr= sp_OASetProperty @objRegexExp, 'IgnoreCase', 1
if @hr = 0
exec @hr= sp_OAMethod @objRegexExp, 'Test', @match OUT, @matchstring
if @hr <>0
begin
return null
end
exec sp_OADestroy @objRegexExp
return @match
end
调用
SELECT * FROM table where dbo.RegexMatch('正则表达式',字段名)=0
如果执行报错 执行这个语句
--开启 Ole Automation Procedures
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ole Automation Procedures';
GO
--关闭 Ole Automation Procedures
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ole Automation Procedures';
GO
--关闭高级选项
sp_configure 'show advanced options', 0;
GO
RECONFIGURE;
GO