Tuesday, November 15, 2011

Run Batch File from SQL or SQL from Batch File

Batch File from SQL
------------------------------------------------------------------------------
Enable xp_cmdshell
Option is in surface area manager, or run script

EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE

Use the xp_cmdshell command
xp_cmdshell '"C:\dbs\IM.bat"'




SQL from batch file

Pre-Requisites:
SQL Management Studio must be installed on the machine you are trying to execute the batch file from.
OR
Install the command line utilities only if you do not want to install Management Studio.
http://www.microsoft.com/en-gb/download/details.aspx?id=36433
-------------------------------------------------------------------
sqlcmd -S localhost\dbinstance -U sa -P password -i "C:\Files\CreateDB.sql"

No comments:

Post a Comment