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