views:

2656

answers:

5

I want to do something like:

exec sproc1 and sproc2 at the same time
when they are both finished exec sproc3

I can do this in dts. Is there a way to do it in transact sql? Or is there a way to do it with a batch script (eg vbs or powershell)?

+4  A: 

You could create a CLR Stored Procedure that (using C#) would call the first two on their own threads, and then block until both are complete... then run the third one.

Are you able to use CLR sprocs in your situation? If so, I'll edit this answer to have more detail.

Timothy Khouri
thanks Timothy, but I guess I'm just lucking for a high level solution : an extra couple of lines of transact sql or batch script code. I just wonder is there a simple alternative to dts - Cindi
cindi
+1  A: 

Do you absolutely need both SPs to be executed in parallel?

With simple CRUD statements within a single SP, I've found SQL S. does a very good job of determining which of them can be run in parallel and do so. I've never seen SQL S. run 2 SPs in parallel if both are called sequentially from a T-SQL statement, don't even know if it's even possible.

Now then, do the DTS really execute them in parallel? It could be it simply executes them sequentially, then calls the 3rd SP after the last finishes successfully.

If it really runs them in parallel, probably you should stick with DTS, but then I'd like to know what it does if I have a DTS package call, say, 10 heavy duty SPs in parallel... I may have to do some testings to learn that myself :D

Joe Pineda
Splitting a single statement into parallel threads is a different topic. DTS can run seperate statements in parallel, and gives you a lot if control over how this is done. However sometimes code is preferable to a tool, which is the reason for the question.
cindi
A: 

You can use SSIS. The benefits of this are that the package can be stored in the SQL Server and easily scheduled there.

From PowerShell or just about any external scripting language, you can use the SQL command line osql or sqlcmd. This technique can also be used to schedule it on the SQL Server by shelling out using xp_cmdshell also.

Cade Roux
SSIS = DTS, the question was how to avoid DTSObviously you can run SQL from a scripting language, the question was how to run SQL statments in parallel.
cindi
+1  A: 

sp _ start _ job

I'm doing a similar thing at the moment, and the only way I've found to avoid using SSIS or some external shell is to split my load routine into 'threads' manually, and then fire a single master sqlagent job which in turn executes as many sp _ start _ job's as I have threads. From that point, they all run autonomously.

It's not exactly what we're looking for, but the result is the same. If you test the job status for the sub jobs, you can implement your conditional start of sproc 3 as well.

What's the point in 8 cores if we can't use them all at once?

Simeon
This is great! When each thread finishes I can check each of the other jobs, so the final thread to finish can fire off the next job.
cindi
A: 

Hope this tool can help you.

Execute T-SQL Scripts in Parallel http://www.sqlservercentral.com/scripts/T-SQL/68002/