• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2318
  • Last Modified:

launch client application from database trigger on MS SQL Server

I want to launch a client application from a database trigger on MS SQL Server. When a table is updated the rigger should launch an application that checks various things. Any Ideas how to do this? I tried by creating a DTS to launch a exe-file on the client computer. It did not work. Could not find path. I wonder if u can use DTS to launch an app remotely or should I use MSMQ. How is this done? I have never used MSMQ before.
  • 5
  • 3
1 Solution
directly from sql help ... its 1 way of doing this ...

xp_cmdshell (T-SQL)

Executes a given command string as an operating-system command shell and returns any output as rows of text. Grants nonadministrative users permissions to execute xp_cmdshell.

Note When executing xp_cmdshell with the Microsoft® Windows® 95/98 operating system, the return code from xp_cmdshell will not be set to the process exit code of the invoked executable. The return code will always be 0.


xp_cmdshell {'command_string'} [, no_output]



Is the command string to execute at the operating-system command shell. command_string is varchar(255) or nvarchar(4000), with no default.


Is an optional parameter executing the given command_string, and does not return any output to the client.

Return Code Values

0 (success) or 1 (failure)

Result Sets

Executing this xp_cmdshell statement returns a directory listing of the current directory.

xp_cmdshell 'dir *.exe'

The rows are returned in an nvarchar(255) column.
Executing this xp_cmdshell statement returns the following result set:

xp_cmdshell 'dir *.exe', NO_OUTPUT

Here is the result:

The command(s) completed successfully.


xp_cmdshell operates synchronously. Control is not returned until the command shell command completes.
When you grant execute permissions to users, the users can execute any operating-system command at the Microsoft Windows NT® command shell that the account running Microsoft SQL Server™ has the needed privileges to execute.
By default, only members of the sysadmin fixed server role can execute this extended stored procedure. You may, however, grant other users permission to execute this stored procedure. Users who are not members of the sysadmin fixed server role will always run their Windows NT commands in the context of the SQLAgentCmdExec user account, which is the same account used by SQL Server Agent for scheduled tasks entered by users who are not members of sysadmin. When members of the sysadmin fixed server role execute xp_cmdshell, their Windows NT commands execute in the MSSQLServer service's security context, which by default is a user account with local administrator authority.

Note In previous versions, a user who was granted execute permissions for xp_cmdshell ran the command in the context of the MSSQLServer service's user account. SQL Server could be configured (through a configuration option) so that users who did not have sa access to SQL Server could run xp_cmdshell in the context of the SQLExecutiveCmdExec Windows NT account. In SQL Server 7.0, the account is called SQLAgentCmdExec. Users who are not members of the sysadmin fixed server role now run commands in the context of this account without specifying a configuration change.


Execute permissions for xp_cmdshell default to members of the sysadmin fixed server role but can be granted to other users.

Important If you choose to use a Windows NT account that is not a member of the local administrator's group for the MSSQLServer service, users who are not members of the sysadmin fixed server role cannot execute xp_cmdshell.

A. Return a list of executable files

This example shows the xp_cmdshell extended stored procedure executing a directory command.

EXEC master..xp_cmdshell 'dir *.exe'

B. Use Windows NT net commands

This example shows the use of xp_cmdshell in a stored procedure. This example notifies users (with net send) that SQL Server is about to be shut down, pauses the server (with net pause), and then shuts the server down (with net stop).

CREATE PROC shutdown10


EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server shutting down
in 10 minutes. No more connections allowed.', no_output
EXEC xp_cmdshell 'net pause sqlserver'
WAITFOR DELAY '00:05:00'
EXEC xp_cmdshell 'net send /domain: SQL_USERS ''SQL Server shutting down
in 5 minutes.', no_output
WAITFOR DELAY '00:04:00'
EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server shutting down
in 1 minute. Log off now.', no_output
WAITFOR DELAY '00:01:00'
EXEC xp_cmdshell 'net stop sqlserver', no_output

C. Return no output

This example uses xp_cmdshell to execute a command string without returning the output to the client.

USE master

EXEC xp_cmdshell 'copy c:\sqldumps\pubs.dmp \\server2\backups\sqldumps',

D. Use return status

In this example, the xp_cmdshell extended stored procedure also suggests return status. The return code value is stored in the variable @result.

DECLARE @result int

EXEC @result = xp_cmdshell 'dir *.exe'

IF (@result = 0)

PRINT 'Success'
PRINT 'Failure'

E. Write variable contents out to file

This example writes the contents of the current directory to a file named dir_out.txt in the current server directory.

DECLARE @cmd sysname, @var sysname

SET @var = 'dir /p'

SET @cmd = 'echo ' + @var + ' > dir_out.txt'

EXEC master..xp_cmdshell @cmd
but msmq is probably a better way of going (leave the other program running and send it a message)
Peter_AnkarlouAuthor Commented:
thanks indeed a lot for all your efforts. The way you have described using XP_CMD is the way I have solved it so far. And it works fine. The problem I have is when my XP_CMD is opening up and running a DTS that launches a MSMQ message. When I try to open the DTS and run it from the SQL Server Enterprise manager it works fine and the program on my computer starts due to the triggers and the rule of the message queue I have set up on my computer but ...
When I insert a row in the table that through a trigger fires a XP_CMD command that in itself fires of the DTS that launches a MSMQ message I get the reply that a message could not be created. I suspect this means that in order to execute the DTS on the server and get the disered result you need to have a MSMQ queue installed on the SQL server and also have sufficient rights to actually send message through the MSMQ server installed on the SQL Server right?
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

well that would be a good set of axioms.
Peter_AnkarlouAuthor Commented:
Gregory, maybe I have stated something obvious, I just would very much like a confirmation that it is true?
remember that xp_cmd runs as the user that sql server is running as ...
was that the issue (command be running as the user of the sqlserver service?)
Peter_AnkarlouAuthor Commented:
No. I wanted help with launching a MSMQ Message from a database trigger that in itself launched an application on another server. I could not solve it due to various security issues regarding lack of AD for the SQL Server forinstance. I gave you the 500 points for your great effort. Thanks. Peter
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now