2011-02-27 38 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