Executing Command Line Application On SQLServer

Sometime other than run sql command on trigger we need to run external application to be ran, please note there is some security concern related activating this feature (disabled by default) and there is a delay on your application due there is a locking as long as the executing command line application.

So first of all let’s activate executing command line from sqlserver this function by run this commands.


EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'xp_cmdshell', 1;
GO
RECONFIGURE;
GO

Explanation:

  • EXEC sp_configure ‘show advanced options’, 1;, Allow advance policy to be changed.
  • EXEC sp_configure ‘xp_cmdshell’, 1;, Enabling xp_cmdshell command.

 

Then you can run command run command xp_cmdshell on your SQLServer, here some example running python script when there is new record on table namely karyawan by creating trigger by name panggil.


CREATE TRIGGER panggil ON karyawan
FOR INSERT
AS
BEGIN
EXEC master..xp_cmdshell 'C:\Python27\python.exe C:/pyscripts/notify.py';
END

 

If you want to delete created trigger below then run this command


DROP TRIGGER panggil;

 

If you want dive deeply about SQLServer’s Trigger then i recommend this page to start.

Advertisements

One thought on “Executing Command Line Application On SQLServer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s