1

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.

4
  • 1
    What do you mean by "is visible"? You are on the SQL Server box and expect to see the rar opened when it is called by SQL Server? No, this is not how it works. The process runs in the background, which is why the programs you call from SQL Server this way can't rely on UI components, prompts, user input, etc. Commented Jul 13, 2012 at 12:22
  • Using the "xp_cmdshell" is not secure because it allows you to do all those OS level things like running external executable.it is recommended to not use "xp_cmdshell". you can get solution from here- mssqltips.com/sqlservertip/2014/… Commented Jul 13, 2012 at 13:51
  • FoA I am so sorry that I was unable to follow you guys on this probelm that i had,,Ok, now I get it,, so we can not view UI components as the exe is executed on the server. I thought its like,, Since its working on my computer the exe is opened on my machine,, which can be viewed what is happening. Commented Jul 16, 2012 at 3:27
  • Thanks a lot Utkarsh :) Commented Jul 23, 2012 at 7:46

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.