Solved

MS SQL Stored Procedures to run an exe program

Posted on 2002-03-12
3
424 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 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

856 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