2017-06-05 330 views
0

我创建了一个调用其他存储过程的存储过程,但我不想同时执行它们。存储过程#1的执行需要时间,因此当它完成存储过程#2时将开始执行。如何在SQL Server中按顺序执行存储过程

我该怎么做?

EXEC SP1; 
--wait for SP1 to finish its job 
EXEC SP2; 

有什么建议吗?

+0

https://stackoverflow.com/questions/44013312/sql-server-create-stored-procedure- that-runs-several-stored-procedures-sequent/44013708#44013708 – Horaciux

+0

https://dba.stackexchange.com/questions/80184/run-multiple-stored-procedures-in-sequence –

+6

它的工作原理与默认相同。你的代码中没有什么需要改变的。 –

回答

0

它应该是越简单

EXEC SP1; 
GO 
EXEC SP2; 
GO 
+5

甚至不需要GO语句。请记住 –

+0

。 ty –

+1

您甚至不能在存储过程中使用'GO',这是问题的上下文 - “调用其他存储过程的存储过程” –

0

尝试使用类似:

BEGIN TRY 
BEGIN TRANSACTION 
exec(@sp1) 
exec(@sp2) 
exec(@sp3) 
COMMIT 
END TRY 
BEGIN CATCH 
IF @@TRANCOUNT > 0 (Validate it) 
ROLLBACK (rollback to sp where you want to) 
END CATCH