我想在存储过程中使用多个INSERT INTO @Table_Variable EXECUTE stored_procedure
语句,并且看到几个不同的错误消息。
执行存储过程时,首先会看到以下错误消息:
消息3915,级别16,状态0,过程“ Sp_Name”,第44行[批处理开始第0行]>无法在INSERT-EXEC语句中使用ROLLBACK语句。
并且在注释掉ROLLBACK
语句后再次执行存储过程时,会出现此错误消息:
消息213,级别16,状态7,过程“ Sp_Name”,第44行[批处理开始第0行]>列名或提供的值数与表定义不匹配。
用于创建存储过程的脚本:
-- 1 of 3 CREATE PROCEDURE [dbo].[_sp_Sun]
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[_sp_Sun]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[_sp_Sun] AS'
END
GO
ALTER PROCEDURE [dbo].[_sp_Sun]
@request_energy_flag BIT
AS
SET NOCOUNT ON;
BEGIN TRY
SET XACT_ABORT ON;
BEGIN TRANSACTION;
IF @request_energy_flag = 1
BEGIN
DECLARE @Table_Energy TABLE
(
id INT
,energy_source NVARCHAR(16)
);
INSERT INTO @Table_Energy (id, energy_source)
SELECT 0, 'Sun';
END;
SELECT id
,energy_source
FROM @Table_Energy;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END;
THROW;
END CATCH;
GO
-- 2 of 3 CREATE PROCEDURE [dbo].[_sp_Mercury]
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[_sp_Mercury]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[_sp_Mercury] AS'
END
GO
ALTER PROCEDURE [dbo].[_sp_Mercury]
@request_energy_flag BIT
AS
SET NOCOUNT ON;
BEGIN TRY
SET XACT_ABORT ON;
BEGIN TRANSACTION;
IF @request_energy_flag = 1
BEGIN
DECLARE @Table_Energy TABLE
(
id INT
,energy_source NVARCHAR(16)
);
INSERT INTO @Table_Energy (id, energy_source)
EXECUTE _sp_Sun @request_energy_flag = 1;
INSERT INTO @Table_Energy (id, energy_source)
SELECT
1, 'Mercury';
END;
SELECT id
,energy_source
FROM @Table_Energy;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END;
THROW;
END CATCH;
GO
-- 3 of 3 CREATE PROCEDURE [dbo].[_sp_Venus]
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[_sp_Venus]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[_sp_Venus] AS'
END
GO
ALTER PROCEDURE [dbo].[_sp_Venus]
@request_energy_flag BIT
AS
SET NOCOUNT ON;
BEGIN TRY
SET XACT_ABORT ON;
BEGIN TRANSACTION;
IF @request_energy_flag = 1
BEGIN
DECLARE @Table_Energy TABLE
(
id INT
,energy_source NVARCHAR(16)
);
INSERT INTO @Table_Energy (id, energy_source)
EXECUTE _sp_Sun @request_energy_flag = 1;
-- Works when this INSERT EXECUTE BLOCK is commented out.
INSERT INTO @Table_Energy (id, energy_source)
EXECUTE _sp_Mercury @request_energy_flag = 1;
--Works when this INSERT EXECUTE BLOCK is commented out.
INSERT INTO @Table_Energy (id, energy_source)
SELECT 2, 'Venus';
END;
SELECT id
,energy_source
FROM @Table_Energy;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END;
THROW;
END CATCH;
GO
这是电话
EXECUTE _sp_Sun @request_energy_flag = 1;
PRINT 'Sun Successful';
EXECUTE _sp_Mercury @request_energy_flag = 1;
PRINT 'Mercury Successful';
EXECUTE _sp_Venus @request_energy_flag = 1;
PRINT 'Venus Successful';