Solved

MS SQL Stored Procedures to run an exe program

Posted on 2002-03-12
3
426 Views
Last Modified: 2012-05-04
Hi,

I'm thinking of using MS SQL stored procedures to run an exe program that help me to create files when a new record is added to a table. Is there a way to achieve this ?

Thanks,
Nick
0
Comment
Question by:n_chai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Expert Comment

by:peterdownes
ID: 6859392
Have a look at the xp_cmdshell system stored procedure.

Regards.
0
 
LVL 7

Accepted Solution

by:
simonsabin earned 200 total points
ID: 6860069
This would do it for you, the location of the file is relative to the SQL Server.

drop table mytable
go
create table myTable (col1 varchar(100))
go
create trigger T_MyTable ON myTable FOR insert
as
DECLARE @Exec varchar(200)
DECLARE @col1 varchar(100)
SET NOCOUNT ON
DECLARE cInserts CURSOR FAST_FORWARD FOR SELECT * FROM inserted
OPEN cINserts
FETCH cInserts INTO @Col1
WHILE @@FETCH_STATUS = 0
  BEGIN
  SET @EXEC = 'echo ' + @col1 + ' >> MyFile.txt'
  exec master..xp_cmdshell @EXEC, no_output
  FETCH cInserts INTO @Col1
  END
DEALLOCATE cInserts
go
insert into myTable values('asdasdas')
insert into myTable values('asdasdas')
insert into myTable values('asdasdas')
insert into myTable
SELECT 'asdasdas'
UNION ALL
SELECT 'asdasdas'
UNION ALL
SELECT 'asdasdas'
UNION ALL
SELECT 'asdasdas'
UNION ALL
SELECT 'asdasdas'
go
exec master..xp_cmdshell 'type MyFile.txt'
0
 
LVL 1

Expert Comment

by:JR_LA
ID: 6861909
this will be a very slow insert statement.

be careful whenever you do things like this adhoc.  should twenty inserts be done on your table that could mean 20 instances of your executable running at the same time.

each insert, if called by a trigger, will have to wait until the execution has completed.

a better approach, if this absolutely must happen, is to use extended stored procedures to take advantage of a proper threading model.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Rebooting Witness SQL Server 2 25
SQL Server Sum Over Multiple Tables 20 33
Want an individual results display div 8 43
SQL Query Across Multiple Tables - Help 5 17
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question