SQL 在存储过程中执行存储过程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/170328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing a stored procedure within a stored procedure
提问by test
I would like to execute a stored procedure within a stored procedure, e.g.
我想在存储过程中执行一个存储过程,例如
EXEC SP1
BEGIN
EXEC SP2
END
But I only want SP1
to finish after SP2
has finished running so I need to find a way for SP1
to wait for SP2
to finish before SP1
ends.
但是我只想在运行SP1
完成后SP2
完成,所以我需要找到一种方法SP1
来等待SP2
结束前完成SP1
。
SP2
is being executed as part of SP1
so I have something like:
SP2
正在执行,SP1
所以我有类似的东西:
CREATE PROCEDURE SP1
AS
BEGIN
EXECUTE SP2
END
回答by Mark Brackett
T-SQL is not asynchronous, so you really have no choice but to wait until SP2 ends. Luckily, that's what you want.
T-SQL 不是异步的,所以你真的别无选择,只能等到 SP2 结束。幸运的是,这就是你想要的。
CREATE PROCEDURE SP1 AS
EXEC SP2
PRINT 'Done'
回答by mattruma
Here is an example of one of our stored procedures that executes multiple stored procedures within it:
这是我们的存储过程之一的示例,该存储过程在其中执行多个存储过程:
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)
回答by Jom George
Inline Stored procedure we using as per our need. Example like different Same parameter with different values we have to use in queries..
我们根据需要使用内联存储过程。例如,我们必须在查询中使用不同值的相同参数。
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
回答by PeteT
Thats how it works stored procedures run in order, you don't need begin just something like
这就是存储过程按顺序运行的方式,您不需要像这样开始
exec dbo.sp1
exec dbo.sp2