前面数据库迁移有遇到sp_OACreate的相关报错,当时修改数据库选项并赋权解决了该问题,但具体是哪边使用了sp_OACreate导致的并没有深究,今天专门研究了一下。
--报错
EXEC sp_OACreate;
SQL Server 阻止了对组件“Ole Automation Procedures”的 过程“sys.sp_OACreate”的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用“Ole Automation Procedures”。有关启用“Ole Automation Procedures”的详细信息,请搜索 SQL Server 联机丛书中的“Ole Automation Procedures”。
--查看参数
EXEC sp_configure 'Ole Automation Procedures';
GO
Ole Automation Procedures 0 1 0 0
--启用 OLE Automation Procedures。
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
--check
Ole Automation Procedures 0 1 1 1
--用户执行赋权
USE [master]
GO
CREATE USER [user_name] FOR LOGIN [user_name]
GO
grant execute on sys.sp_OACreate to user_name
go
----------------USE sp_OACreate
-- sp_OACreate和SQL Server中执行正则表达式
SQL Server中执行正则表达式
-- =============================================
-- Description: <sqlServer自定义正则表达式函数>
-- =============================================
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
end
-- =============================================
--测试语句(假如要查找所有用户名为纯英文的用户内):
select userName, dbo.Reg('^[a-zA-Z]+$',userName) isPureEnglish
from (values('Tomas'),('Jeson'),('Jerry'),('007'),('$$$'),('Tomas1st')) as D(userName)
/*
userName isPureEnglish
Tomas 1
Jeson 1
Jerry 1
007 0
$$$ 0
Tomas1st 0
*/