Xp_cmdshell

  • Hi,

    i am trying to create a xp_cmdshell to execute the DBCC checkDB comand and same the output to a txt file. Can someone help, please?

    Thank you.

  • You don't need to use xp_cmdshell for this. Just run DBCC CHECKDB directly inside a query window in SSMS and select the option to output it to a file.

    Or is there another reason why xp_cmdshell is wanted here?

    Tara Kizer
    Microsoft MVP for Windows Server System - SQL Server
    Ramblings of a DBA (My SQL Server Blog)[/url]
    Subscribe to my blog

  • yes, there is , i am running this command from a VB application (nor in query window).

    I have achieved this goal yesterday.

    Thank you for the reply.

  • If you are using a VB application, then there is no need to use xp_cmdshell for this.

    Tara Kizer
    Microsoft MVP for Windows Server System - SQL Server
    Ramblings of a DBA (My SQL Server Blog)[/url]
    Subscribe to my blog

  • use SQLCMD or something.

  • Tara Kizer (7/13/2010)


    If you are using a VB application, then there is no need to use xp_cmdshell for this.

    Since I'm no VB expert but still very curious, how would you do it from VB?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • chauhanparixit (7/3/2013)


    use SQLCMD or something.

    How do you do that from VB?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (7/3/2013)


    Tara Kizer (7/13/2010)


    If you are using a VB application, then there is no need to use xp_cmdshell for this.

    Since I'm no VB expert but still very curious, how would you do it from VB?

    it's nothing more than creating a connection to the server so you can run the same commands you would in SSMS;

    an application would never call xp_cmdshell; xp_cmdshell is specific to SQL Server, and how it interacts with the operating system.

    that would be like using word to embed an excel spreadhseet with a macro to open word.

    I'm confused, like others, as to why need to have SQL Server call an application to do work in SQL server...that's just adding intermediate steps, which can fail for a variety of reasons, when it can be done directly.

    anyway, some code examples, because, well, that's what i do!

    'VB.NET

    Dim sqlcmd As String = "DBCC CHECKDB('SANDBOX') WITH DATA_PURITY"

    Dim mySqlConnectionFormat As String = "data source={0};initial catalog={1};user id={2};password={3};Trusted_Connection=False;Connect Timeout=600;Application Name=GhostInTheMachine;"

    Dim MyConn As New SqlConnection

    MyConn.ConnectionString = String.Format(mySqlConnectionFormat, ".", "SandBox", "Noobie", "NotARealPassword")

    MyConn.Open()

    'now lets get a command object

    Dim mySqlCommand As New SqlCommand

    mySqlCommand.Connection = MyConn

    mySqlCommand.CommandTimeout = 600

    mySqlCommand.CommandType = CommandType.Text

    mySqlCommand.CommandText = sqlcmd

    mySqlCommand.ExecuteNonQuery()

    'vb6

    Dim sqlcmd As String

    sqlcmd = "DBCC CHECKDB('SANDBOX') WITH DATA_PURITY"

    Dim myConn As New ADODB.Connection

    Dim MyConnectionString As String

    MyConnectionString ="PROVIDER=SQLOLEDB;DATA SOURCE=MyServer;UID=Noobie;PWD=NotARealPassword;DATABASE=SandBox;"

    myConn.ConnectionString = MyConnectionString

    myConn.CursorLocation = adUseClient

    myConn.CommandTimeout = 600

    myConn.ConnectionTimeout = 600

    myConn.Open

    myConn.Execute(sqlcmd)

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • @lowell,

    Thank you for the coded example. Much appreciated. That's kind of what I thought it was going to be but haven't touched VB since 2002 and didn't want to hazard a guess. Essentially, a "simple" result set.

    I wouldn't allow the app to call DBCC CheckDB directly, though. It requires either DBO or SA privs and it's extremely unlikely (there's always the tooth of a chicken exception) that I'd allow an app to have that level of privs. I'd likely build a stored proc that did the deed using WITH EXECUTE AS OWNER and grant the app privs to run the proc (along with the appropriate delousing for SQL Injection, etc., in the stored proc).

    I also wonder why the OP wants this output to be written to a file, to being with.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • river1 (7/12/2010)


    Hi,

    i am trying to create a xp_cmdshell to execute the DBCC checkDB comand and same the output to a txt file. Can someone help, please?

    Thank you.

    Please see above. As folks have stated, you don't need xp_CmdShell for this. You should also (as stated above) not call DBCC CheckDB directly from the app so that the app doesn't require inappropriately high privs. Rather, create a stored proc to execute with the owner privs and give the app privs to run that proc. An app with even just PUBLIC privs will be able to run the proc without having the ability to run anything that could actually cause grave damage to the server.

    My question would be, why do you want to save the output of this command in a file on the files system? What will you do with that file afterwards?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply