views:

476

answers:

6

Is it possible to run multiple stored procedures that run 'in the background'?

The stored procedures must be launched from a single, master stored procedure, in the same way that multiple worker threads are spawned. For example:

CREATE PROCEDURE MyLauncher
AS
BEGIN
    BEGIN
      @EXEC MyBackgroundSP01 -- Runs in parallel to the other 2
      @EXEC MyBackgroundSP02 -- Runs in parallel to the other 2
      @EXEC MyBackgroundSP03 -- Runs in parallel to the other 2
    END
END
+2  A: 

No this isn't possible as you have described it. You could run multiple SQL Jobs which will execute the procedures concurrently/

JoshBerke
The procedure has to run on MS SQL Express Edition, so Jobs can't be used. I'll amend the question to mention this.
Thomas Bratt
Whats calling the procedure? Why not have the caller handle the threading?
JoshBerke
SQL Server is calling the procedure on startup. I can always work around this by getting SQL Server to call the procedures individually but I thought I would check to see if there was a more elegant way.I have a feeling I'm trying to do something that would break the ACID principles for transactions 8)
Thomas Bratt
It is possible with Express using straight T-SQL: http://rusanu.com/2009/08/05/asynchronous-procedure-execution/
Remus Rusanu
A: 

This could depend on your hardware setup. For example, how many processors sit on the machine/server which hosts SQL Server?

Though the stored procs would run concurrently as mentioned in the previous post

kevchadders
The procedures won't run in parallel. SQL can run a query in parallel but it wouldn't run two seperate statements in parallel.
JoshBerke
A: 

According to this question you could try using the Service Broker

http://stackoverflow.com/questions/25460/asynchronous-stored-procedure-calls

OG
A: 

If you run they in the same procedure, it will launch in the same thread (and in the same internal transaction, what can make the log very big).

j.a.estevan
A: 

Not with pure T-SQL. But you could write a little dotNET app to run them asynchronously easily enough, as long as you leave the connection option until all three are finished.

CodeByMoonlight
+2  A: 

It is possible in SQL 2005 and later. Have look at http://rusanu.com/2009/08/05/asynchronous-procedure-execution/

Remus Rusanu