|
Question : How can execute an application asynchronously in a trigger/stored procedure?
|
|
I've written a trigger which I want to fire up an executable when it is run. Using at the moment: @Cmd = C:\AppPath\App.exe -arguement1 xp master..xp_cmdshell @Cmd
The problem being that it seems to be waiting for the execution of the app to finish before continuing execution of the trigger - I just want it to execute it and then forget about it, and just carry on with its own tasks,. How can i Do this?
SQL Server 2000 (8.0) running on windows server 2003.
|
|
Answer : How can execute an application asynchronously in a trigger/stored procedure?
|
|
xp_cmdShell is synchronous, but you can fool it if you make a simple application to start a process and then to exit. I did it the following way (in VB.Net 2005): Create new Console Application project, name it StartProcess and copy the following code:
Module Module1 Function Main(ByVal CmdArgs() As String) As Integer Dim myProcess As New Process, i As Integer If CmdArgs.Length = 0 Then Return -200 Try myProcess.StartInfo.FileName = CmdArgs(0) For i = 1 To CmdArgs.Length - 1 myProcess.StartInfo.Arguments = myProcess.StartInfo.Arguments & " " & CmdArgs(i) Next myProcess.StartInfo.CreateNoWindow = True myProcess.Start() Catch Return -100 End Try Return 0 End Function End Module
Compile and copy exe to (for example) c:\ Test call from SQL: EXEC xp_cmdshell 'c:\startprocess cmd /c c:\test.bat', no_output; (my c:\test.bat was - just to last very long dir c:\ /s > c:\zz.txt exit)
Hope you'll find this info helpful.
|
|
|
|