Solved

MS SQL Stored Procedures to run an exe program

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now