Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SQL Server 2008 Books Online Jan, 2009 download now availableSQL Server Stored Procedure with nvarchar parameter »
    comments

    If you try to execute xp_cmdshell on a fresh install of SQL Server 2005 or 2008 you will be greeted with the following message

    Server: Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1
    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.

    This is done for security reasons. I see a bunch of Google searches hitting our site with a search like this: How do I enable xp_cmdshell in SQL Server 2005? Well you can do it two ways; one is with a script and the other with the Surface Configuration Tool
    Let's start with the Surface Configuration Tool

    Surface Configuration Tool
    You have to navigate to the tool from the start button, the path is below
    Programs-->Microsoft SQL server 2005-->Configuration Tools-->Surface Configuration Tool

    Then select Surface Area Configuration for Features (bottom one)
    Expand Database Engine go all the way down to xp_cmdshell and click enable xp_cmdshell and hit apply

    SQL Script
    To do it with a script use the one below

    1. EXECUTE sp_configure 'show advanced options', 1
    2. RECONFIGURE WITH OVERRIDE
    3. GO
    4.  
    5. EXECUTE sp_configure 'xp_cmdshell', '1'
    6. RECONFIGURE WITH OVERRIDE
    7. GO
    8.  
    9. EXECUTE sp_configure 'show advanced options', 0
    10. RECONFIGURE WITH OVERRIDE
    11. GO

    Just so you know you can also use the script to enable xp_cmdshell on SQL Server 2008. The Surface Area Configuration Tool in SQL Server 2008 has been replaced by the SQL Server Configuration Manager.

    In SQL Server 2005 and 2008 OPENROWSET is also disabled by default, if you try to run an OPENROWSET query then you will see the following message:

    Server: Msg 15281, Level 16, State 1, Line 1
    SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.

    To enable OPENROWSET and OPENQUERY you can use the previous script but instead of 'xp_cmdshell' you will use 'Ad Hoc Distributed Queries'. The script to enable Ad Hoc Distributed Queries is below

    1. EXECUTE SP_CONFIGURE 'show advanced options', 1
    2. RECONFIGURE WITH OVERRIDE
    3. GO
    4.  
    5. EXECUTE SP_CONFIGURE 'Ad Hoc Distributed Queries', '1'
    6. RECONFIGURE WITH OVERRIDE
    7. GO
    8.  
    9. EXECUTE SP_CONFIGURE 'show advanced options', 0
    10. RECONFIGURE WITH OVERRIDE
    11. GO




    *** If you have a SQL related question try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    28379 views
    Instapaper

    11 comments

    Comment from: Susan [Visitor]
    *****
    Susan thanks! Script worked like a charm in 2008 - saved for future reference.
    07/20/09 @ 16:05
    Comment from: king marcus dizon [Visitor]
    king marcus dizon thanks very musch ...!!!
    12/23/09 @ 04:16
    Comment from: Steve Daigle [Visitor]
    Steve Daigle Thanks!
    12/23/09 @ 14:44
    Comment from: Denise [Visitor]
    Denise Can you tell me where to run the script for enabling the xp_cmdshell? I previously used the Surface Area Config with 2005...but we upgraded to SQL 2008 Express and that is no longer there. I dont know where to run the TSQL from?
    12/28/09 @ 20:35
    Comment from: SQLDenis [Member] Email
    SQLDenis Denise, you need to run it from a query window
    12/28/09 @ 20:36
    Comment from: Cameron Singe [Visitor] · http://cameronsinge.com
    Cameron Singe Thanks mate, that helped heaps
    05/23/10 @ 19:18
    Comment from: Barry Chadwick [Visitor]
    Barry Chadwick Hi, I have just installed SQL 2008 on my new machine after using SQL 2005 on my old one. I was trying to connect to a remote instance of 2005 on another server from my new 2008 instance and it was erroring as you described, even though "Allow Remote Connections to this server" was checked on my instance properties.

    I ran your script and now it's working fine.

    Cheers
    06/01/10 @ 09:47
    Comment from: Grady Christie [Visitor] · http://www.centralfreight.com
    Grady Christie Thank you very much. This worked like a charm for me.
    06/05/10 @ 13:02
    Comment from: D Anderson [Visitor]
    D Anderson This was great I'm and Engineer, not a DBA, that works with Control systems. We are cutting cost by using 2008 express as 10GB is more than enough for Data Storage. Since Surface Area Configurator (What ever the Hell that is) is not with Express I was glad to find the info to set XP_cmd and ad Hoc. Thank you very much.
    07/16/10 @ 14:01
    Comment from: Vijeth [Visitor]
    Vijeth Thanks a TON!!! i was wondering how to setup the XP_cmdshell. I had provided bulkadmin access to the ID and it was still not working. Thanks for the quick win solution.
    11/03/10 @ 04:08
    Comment from: Ni10 [Visitor] Email
    Ni10 Hi,
    How to use “sp_configure to set some common Advanced Options” in STORED PROCEDURE?
    In my Procedure i have to use some common Advanced Options in “SP_CONFIGURE”.
    but there is following error Occurred at every time when i execute.
    Error is : “Msg 15281, Level 16, State 1, Procedure PrExportToExcel, Line 40
    SQL Server blocked access to STATEMENT ‘OpenRowset/OpenDatasource’ of component ‘Ad Hoc Distributed Queries’ because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of ‘Ad Hoc Distributed Queries’ by using sp_configure. For more information about enabling ‘Ad Hoc Distributed Queries’, see “Surface Area Configuration” in SQL Server Books Online. ”

    i have solved this error but when i was alter my given procedure again this error is occurred.
    The Problem is
    i’m using following command in between #temp in Given Procedure.
    exec sp_configure ‘show advanced options’, 1;
    RECONFIGURE WITH OVERRIDE
    exec sp_configure ‘Ad Hoc Distributed Queries’, 1;
    RECONFIGURE WITH OVERRIDE

    Please any one know, how i can use “sp_configure ” in my procedure.
    then please reply immediately.

    Thnx in Advance
    08/02/11 @ 23:37

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)