SQL存储过程调用另一个存储过程

偷师于吉普赛的歌。。。

直接上代码了

USE tempdb
GO
-- 定义中转表,结构与你需要输出的一致,但需要增加 sessionId 和 addTime,
-- sessionId 防止并发时出现无法辨别的问题
-- addTime 记录插入时间,便于观察
IF OBJECT_ID('dbo.tmp_transfer1') IS NOT NULL DROP TABLE tmp_transfer1
IF OBJECT_ID('dbo.tmp_transfer2') IS NOT NULL DROP TABLE tmp_transfer2
GO
CREATE TABLE dbo.tmp_transfer1(sessionId VARCHAR(50),addTime DATETIME, id INT,n VARCHAR(50))
CREATE TABLE dbo.tmp_transfer2(sessionId VARCHAR(50),addTime DATETIME, id INT,n VARCHAR(50))
GO
IF OBJECT_ID('Proc_A') IS NOT NULL DROP PROC Proc_A
IF OBJECT_ID('Proc_B') IS NOT NULL DROP PROC Proc_B
GO
-- =============================================
-- Author:		yenange
-- Create date: 2018-08-20
-- Description:	
-- =============================================
CREATE PROCEDURE dbo.Proc_B
@sessionId VARCHAR(50)
AS
BEGIN
	SET NOCOUNT ON;
	INSERT INTO dbo.tmp_transfer1
	(
		sessionId,
		addTime,
		id,
		n
	)
	SELECT 
		@sessionId
		, GETDATE()
		, sv.number
		, NEWID()
	FROM MASTER.dbo.spt_values sv
	WHERE sv.type='P' AND sv.number BETWEEN 1 AND 3
	
	INSERT INTO dbo.tmp_transfer2
	(
		sessionId,
		addTime,
		id,
		n
	)
	SELECT 
		@sessionId
		, GETDATE()
		, sv.number
		, NEWID()
	FROM MASTER.dbo.spt_values sv
	WHERE sv.type='P' AND sv.number BETWEEN 10 AND 13
END
GO
-- =============================================
-- Author:		yenange
-- Create date: 2018-08-20
-- Description:	
-- =============================================
CREATE PROCEDURE dbo.Proc_A
AS
BEGIN
	SET NOCOUNT ON;
	
	DECLARE @guid VARCHAR(50)
	SET @guid = NEWID();
	
	--执行操作,将数据存放到 中转表
	EXEC Proc_B @guid
	
	--根据得到的数据,执行下一步的处理
	SELECT * FROM dbo.tmp_transfer1 WHERE sessionId=@guid;
	SELECT * FROM dbo.tmp_transfer2 WHERE sessionId=@guid;
	
	--清除产生的数据
	DELETE FROM  dbo.tmp_transfer1 WHERE sessionId=@guid;
	DELETE FROM  dbo.tmp_transfer1 WHERE sessionId=@guid;
END
GO

EXEC Proc_A

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页