tes*_*est 42 sql t-sql sql-server stored-procedures sql-server-2005
我想在存储过程中执行存储过程,例如
EXEC SP1
BEGIN
EXEC SP2
END
Run Code Online (Sandbox Code Playgroud)
但是我只想SP1在SP2完成跑步后完成所以我需要找到一种SP1等待SP2在结束前完成的方法SP1.
SP2正在执行,SP1所以我有类似的东西:
CREATE PROCEDURE SP1
AS
BEGIN
EXECUTE SP2
END
Run Code Online (Sandbox Code Playgroud)
Mar*_*ett 34
T-SQL不是异步的,所以你别无选择,只能等到SP2结束.幸运的是,这就是你想要的.
CREATE PROCEDURE SP1 AS
EXEC SP2
PRINT 'Done'
Run Code Online (Sandbox Code Playgroud)
mat*_*uma 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)
Run Code Online (Sandbox Code Playgroud)
Jom*_*rge 11
我们根据需要使用的内联存储过程.像不同的示例相同的参数,我们必须在查询中使用不同的值.
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
Run Code Online (Sandbox Code Playgroud)
这就是存储过程按顺序运行的工作原理,您不需要像这样开始
exec dbo.sp1
exec dbo.sp2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
208464 次 |
| 最近记录: |