我想执行 EXEC master..xp_cmdshell @bcpquery
但是我收到以下错误:
SQL Server阻止访问组件"xp_cmdshell"的过程"sys.xp_cmdshell",因为此组件已作为此服务器的安全配置的一部分关闭.系统管理员可以使用sp_configure启用"xp_cmdshell".有关启用"xp_cmdshell"的详细信息,请参阅SQL Server联机丛书中的"表面区域配置".
有没有办法激活它,或在启用该功能之前执行某些操作?
怎么解决?
Per*_* P. 360
你需要启用它.查看xp_cmdshell MSDN文档的Permission部分:
http://msdn.microsoft.com/en-us/library/ms190693.aspx:
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
Run Code Online (Sandbox Code Playgroud)
hog*_*gar 39
重新配置后,您还可以再次隐藏高级选项:
-- show advanced options
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
-- enable xp_cmdshell
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
-- hide advanced options
EXEC sp_configure 'show advanced options', 0
GO
RECONFIGURE
GO
Run Code Online (Sandbox Code Playgroud)
在其他的答案上市,招(在SQL 2005或更高版本)是改变全局配置设置show advanced options和xp_cmdshell到1,在这个顺序.
除此之外,如果要保留以前的值,可以先从中读取它们sys.configurations,然后在最后以相反的顺序应用它们.我们也可以避免不必要的reconfigure电话:
declare @prevAdvancedOptions int
declare @prevXpCmdshell int
select @prevAdvancedOptions = cast(value_in_use as int) from sys.configurations where name = 'show advanced options'
select @prevXpCmdshell = cast(value_in_use as int) from sys.configurations where name = 'xp_cmdshell'
if (@prevAdvancedOptions = 0)
begin
exec sp_configure 'show advanced options', 1
reconfigure
end
if (@prevXpCmdshell = 0)
begin
exec sp_configure 'xp_cmdshell', 1
reconfigure
end
/* do work */
if (@prevXpCmdshell = 0)
begin
exec sp_configure 'xp_cmdshell', 0
reconfigure
end
if (@prevAdvancedOptions = 0)
begin
exec sp_configure 'show advanced options', 0
reconfigure
end
Run Code Online (Sandbox Code Playgroud)
请注意,这依赖于SQL Server 2005或更高版本(原始问题是2008年).
虽然接受的答案在大多数情况下都有效,但我遇到过(仍然不知道为什么)一些情况却不起作用。WITH OVERRIDE使用in对查询进行轻微修改RECONFIGURE即可得到解决方案
Use Master
GO
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
GO
Run Code Online (Sandbox Code Playgroud)
预期输出是
配置选项“显示高级选项”从 0 更改为 1。运行 RECONFIGURE 语句进行安装。
配置选项“xp_cmdshell”从 0 更改为 1。运行 RECONFIGURE 语句进行安装。