Thursday, February 5, 2009

How to call external program to execute from SQL

I was having requirement from user which I need to execute a program everytime a certain record is inserted into SQL with parameter from SQL. After doing some research I found out how to call external program from SQL in this article.

http://msdn.microsoft.com/en-us/library/ms175046(SQL.90).aspx

What I need to achieve will be something similar to this example. I found this very useful.

Writing variable contents to a file
The following example writes the contents of the @var variable to a file named var_out.txt in the current server directory.

Copy Code
DECLARE @cmd sysname, @var sysname
SET @var = 'Hello world'
SET @cmd = 'echo ' + @var + ' > var_out.txt'
EXEC master..xp_cmdshell @cmd