0

I would like to use multiple INSERT INTO @Table_Variable EXECUTE stored_procedure statements in a stored procedure and I am seeing a couple of different error messages.

When the stored procedure is executed, the following error message is seen first:

Msg 3915, Level 16, State 0, Procedure "Sp_Name", Line 44 [Batch Start Line 0] > Cannot use the ROLLBACK statement within an INSERT-EXEC statement.

and when the stored procedure is executed again after commenting out the ROLLBACK statement, this error message occurs:

Msg 213, Level 16, State 7, Procedure "Sp_Name", Line 44 [Batch Start Line 0] > Column name or number of supplied values does not match table definition.

Script to create stored procedures:

-- 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

Here is the call

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';

Here is what I am hoping the result will be Expected Result

And here is what is happening enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AeyJey
  • 2,111
  • 2
  • 14
  • 21
  • Both error messages appear to be quite clear here, which is it you're not sure on? You should really be declaring your columns in your `INSERT` clause; I suspect if you do so this error goes away. – Thom A Jul 01 '19 at 17:27
  • Go through the below link. That should help https://stackoverflow.com/questions/3795263/errors-insert-exec-statement-cannot-be-nested-and-cannot-use-the-rollback-s – AB_87 Jul 02 '19 at 01:29

0 Answers0