Solved

MS SQL Stored Procedures to run an exe program

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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…

724 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