SqlServer中使用正则表达式

1、查看配置,若是‘show advanced options’,‘Ole Automation Procedures’,‘Ad Hoc Distributed Queries’的config_value和run_value不为1,须要执行二中脚本。数据库

2、执行脚本,打开SQL Server 对组件的访问阻止。函数

USE master
GO
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE;
GO 测试

 

3、建立函数(除数据库名和函数名外其余保持不变便可)blog

Use EDRMSOfficeDB;--(数据库名)
GO
if object_id(N'dbo.Reg') is not null --(函数名)
drop function dbo.Reg;
GO
CREATE FUNCTION Reg
(
@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
endip

4、测试get

查询字段中含有出字母和数字以外的记录:string

SELECT CategoryId FROM Category WHERE dbo.Reg('^[A-Za-z0-9]+$',CategoryId )=0it

相关文章
相关标签/搜索