diyoda_
diyoda_

Reputation: 5420

Run executable from SQL Server

I have been stuck on a research on running an external exe file. What I have so far found was it can be done using this:

EXEC master..xp_cmdshell '"C:\New Folder\test.exe"'

And also this must not be directly called in a trigger because it has to wait until the execution is done to complete the trigger.

So the encouraged approach for this is to have a scheduled job to poll for the table and call the .exe file from there to without creating any performance issues. So far I have accepted it and working on it.

So, before trying on this I am working on each part that has to be learned before implementing.I am testing the above piece of code keeping the database as master. I have tried several more.

EXEC master..xp_cmdshell '"C:\New Folder\r.rar"'

EXEC master..xp_cmdshell '"C:\New Folder\text.text"'

So, I am thinking of this xp_cmdshell as a normal command prompt. I was expecting to be visible the exe file opening and opening of the tet file and the rar file. But its not working.

I have given the above details to tell my approach, Please give me a feed back if you have a better approach in your earlier experiences. Thanks in advance.

Upvotes: 1

Views: 41784

Answers (2)

Anuj Masand
Anuj Masand

Reputation: 359

Try Trigger

Following is the code to run .exe file from a trigger:

CREATE TRIGGER trgAfterInsert on dbo.table_Demo
DECLARE @CMDSQL VARCHAR(1000)

set path of your exe file and can include argument file if needed.

SET @CMDSQL = 'cmd.exe /C "D:\Phoenix restart\612 League\code\PhoenixMallTest\bin\Release\PhoenixMallTest.Console.exe" App.ini'
Exec master..xp_cmdshell @CMDSQL
PRINT 'AFTER INSERT trigger fired.'

If sql server blocks xp_cmdshell or says to enable xp_cmdshell, you can do as following.

Use Master
GO

EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO

EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
GO

Upvotes: 5

What type of error you getting?

You can get this problem because of component is turned off as part of the security configuration for your sql server.

A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.

Upvotes: 1

Related Questions