2011-02-27 470 views
147

我想执行EXEC master..xp_cmdshell @bcpquery启用“xp_cmdshell的” SQL服务器

但我收到以下错误:

SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.

有什么办法来激活此功能,或执行启用该功能之前的东西吗?

如何解决?

回答

308

您需要启用它。退房xp_cmdshell MSDN docs的允许部分:

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 
+1

工作真的很简单有效! – indofraiser 2015-11-09 10:38:51

+0

确保您以管理员身份执行SQL Management Studio – 2016-06-08 06:27:06

31

您还可以隐藏再次高级选项重新配置后:

-- 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 
7

在其他的答案上市,招(在SQL 2005或更高版本)将按此顺序将show advanced optionsxp_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 

注意,这依赖于SQL Server 2005的版本或更高版本(原来的问题是在2008年)。

4

尽管接受的答案在大多数情况下都能正常工作,但我遇到过(仍然不知道为什么)某些情况并非如此。通过使用RECONFIGUREWITH OVERRIDE查询的轻微的修改给出了解决方案

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 

预期的输出是

Configuration option 'show advanced options' changed from 0 to 1. Run the RECONFIGURE statement to install.
Configuration option 'xp_cmdshell' changed from 0 to 1. Run the RECONFIGURE statement to install.

10

右键单击服务器 - >刻面 - >外围应用配置器 - > XPCmshellEnbled - > true enter image description here

-1

您可以使用SQLcmd。 您已经运行以下命令。 enter image description here

+2

您能否显示此答案与此问题的其他答案不同?另外,请使用代码块代替屏幕截图。 – Athafoud 2017-04-11 09:14:55

+0

c:\> sqlcmd -S。 -E 1)EXEC sp_con \ – 2017-04-15 14:37:13