48

我想在存储过程中执行存储过程,例如

EXEC SP1

BEGIN

EXEC SP2
END

但我只想在完成运行SP1SP2完成,所以我需要找到一种方法SP1来等待SP2结束前完成SP1

SP2正在执行,SP1所以我有类似的东西:

CREATE PROCEDURE SP1
AS
BEGIN

EXECUTE SP2

END
4

4 回答 4

41

T-SQL 不是异步的,所以你真的别无选择,只能等到 SP2 结束。幸运的是,这就是你想要的。

CREATE PROCEDURE SP1 AS
   EXEC SP2
   PRINT 'Done'
于 2008-10-04T13:50:13.423 回答
15

这是我们的一个存储过程的示例,它在其中执行多个存储过程:

ALTER PROCEDURE [dbo].[AssetLibrary_AssetDelete]
(
    @AssetID AS uniqueidentifier
)
AS

SET NOCOUNT ON

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

EXEC AssetLibrary_AssetDeleteAttributes @AssetID
EXEC AssetLibrary_AssetDeleteComponents @AssetID
EXEC AssetLibrary_AssetDeleteAgreements @AssetID
EXEC AssetLibrary_AssetDeleteMaintenance @AssetID

DELETE FROM
    AssetLibrary_Asset
WHERE
    AssetLibrary_Asset.AssetID = @AssetID

RETURN (@@ERROR)
于 2008-10-04T13:48:10.497 回答
12

我们根据需要使用内联存储过程。例如,我们必须在查询中使用具有不同值的不同相同参数。

Create Proc SP1
(
 @ID int,
 @Name varchar(40)
 -- etc parameter list, If you don't have any parameter then no need to pass.
 )

  AS
  BEGIN

  -- Here we have some opereations

 -- If there is any Error Before Executing SP2 then SP will stop executing.

  Exec SP2 @ID,@Name,@SomeID OUTPUT 

 -- ,etc some other parameter also we can use OutPut parameters like 

 -- @SomeID is useful for some other operations for condition checking insertion etc.

 -- If you have any Error in you SP2 then also it will stop executing.

 -- If you want to do any other operation after executing SP2 that we can do here.

END
于 2012-10-16T11:56:14.597 回答
3

这就是它的工作方式存储过程按顺序运行,你不需要开始像

exec dbo.sp1
exec dbo.sp2
于 2008-10-04T13:34:42.583 回答