xp_cmdshell execute scripts that are on c:

  • Hi,

    I need to use xp_cmdshell to execute a stored procedure that is my c:

    The name of the stored procedure is Script22.sql

    I have seen the following example on the internet:

    exec master.dbo.xp_cmdshell 'osql -E -Sserver1 -i c:\tempightly.sql'

    But i think that in SQL Server 2005 the OSQL was replace by CMDSQL, am i correct?

    How can i alter this command so that it runs the script in cmdSQL and if the script raises any error, i can catch it?

    Thank you

  • Not sure about catching errors, but as far as running it under SQL 2005 (and 2008), you can substitute "sqlcmd" for "osql". Of course "osql" still works for now (it is deprecated), it is "isql" that was removed.


    Have Fun!
    Ronzo

  • Havent tried it but would assume that you would pipe to an output file. something like:

    sqlcmd -S . -d master -Q "select name from sys.databases" -o "c:\output.csv"

    But obviously inside a XP_cmd... statement.

    Hope this helps. 🙂

    Adam Zacks-------------------------------------------Be Nice, Or Leave

  • Schadenfreude-Mei (7/29/2010)


    Havent tried it but would assume that you would pipe to an output file. something like:

    sqlcmd -S . -d master -Q "select name from sys.databases" -o "c:\output.csv"

    But obviously inside a XP_cmd... statement.

    Hope this helps. 🙂

    SO in your case:

    EXEC xp_cmdshell 'sqlcmd -S . -d master -i "c:\sqlscript.sql" -o "c:\output.csv"'

    Or something like that. Simples (squeeky meercat noise!)

    Adam Zacks-------------------------------------------Be Nice, Or Leave

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

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