storedprocedure to run a sql file(abc.sql) and store result in a file(abc.txt)

  • Can someone please provide a script\storedprocedure to perform below steps.

    1.connect to sql instance

    2.Run a sql file(C:\abc.sql)

    3.Store result in a file(C:\abc.txt)

    thanks

  • sqlcmd -S servername -E -d Databasename -i c:\abc.sql -o c:\abc.txt

  • Steve Jones - SSC Editor (2/4/2016)


    sqlcmd -S servername -E -d Databasename -i c:\abc.sql -o c:\abc.txt

    You can also substitute a ">" for the "-o". It will cause the file to be overwritten if it already exists. Substituting ">>" will cause the file to be appended with the new information.

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

  • Yep, Jeff is right that you can use redirection from a command line.

    If you need to do this inside SQL Server, you can use xp_cmdshell to call this in a shell.

Viewing 4 posts - 1 through 3 (of 3 total)

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