Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS SQL Stored Procedures to run an exe program

Posted on 2002-03-12
3
Medium Priority
?
443 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
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 600 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

Independent Software Vendors: 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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

783 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