SQL – Running Batch File Using T-SQL – xp_cmdshell bat file

What is the usage of xp_cmdshell and
How to execute BAT file using T-SQL?

I really like the follow up questions of my posts/articles. Answer is xp_cmdshell can execute shell/system command, which includes batch file.

1) Example of running system command using xp_cmdshell is:

EXEC master..xp_CMDShell 'ISQL -L'

2) Example of running batch file using T-SQL

Running standalone batch file (without passed parameters)

EXEC master..xp_CMDShell 'c:findword.bat'

Running parameterized batch file

DECLARE @PassedVariable VARCHAR(100)
DECLARE @CMDSQL VARCHAR(1000)
SET @PassedVariable = 'SqlAuthority.com'
SET @CMDSQL = 'c:findword.bat' + @PassedVariable
EXEC master..xp_CMDShell @CMDSQL
SOURCE

LINK

LANGUAGE
ENGLISH