开始不匹配的BEGIN和END错误。但每个BEGIN都有一个END,反之亦然

时间:2019-06-20 11:21:32

标签: sql sql-server stored-procedures

我检查了一下,似乎找不到BEGIN和END以及BEGIN事务和COMMIT中的不匹配。关于RETURNs,我是否不了解什么?或者由于我在存储过程中调用多个存储过程,这是否导致嵌套事务,并且是导致错误的原因?

我正在设计一个后端,该后端允许应用程序存储并跟踪组织内各个部门之间的空位移动,例如草稿。我有一个存储过程,用于当您创建一个新部门时,向您提供您希望这个新部门拥有多少个插槽,并在“插槽”表中生成插槽,然后将它们添加到DepartmentSlots表中,从而将它们链接到刚刚创建的部门创建。

-- ================================================================================
-- Add Department
-- ================================================================================
GO
    CREATE PROCEDURE uspAddDepartment       
        @DepartmentName                 VARCHAR(50),
        @AllocatedSlots                 DECIMAL(18, 2) 

    AS
        SET NOCOUNT ON;
        SET XACT_ABORT ON;

        BEGIN TRANSACTION
            -- See if the department Exists
            DECLARE @DepartmentExists       AS BIT = (SELECT CASE WHEN EXISTS (SELECT @DepartmentName FROM [HeadCount_Ver01].[dbo].Departments WHERE UPPER(DepartmentName) = UPPER(@DepartmentName)) THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END)
            IF (@DepartmentExists = 0)
                BEGIN

                    INSERT INTO [HeadCount_Ver01].[dbo].Departments(DepartmentName, AllocatedSlots)
                    VALUES(@DepartmentName, @AllocatedSlots);

                    DECLARE @DepartmentID AS INTEGER = (SELECT MAX(DepartmentID) FROM Departments)

                    -- Now that we have created a department with newly allocated slots, we have to create those records in DepartmentSlots, and Slots
                    EXECUTE uspGenerateSlots @DepartmentID, @AllocatedSlots;
                END
            ELSE
                BEGIN
                    SELECT 'DEPARTMENT EXISTS'
                END
        COMMIT;
GO


-- ================================================================================
-- Add Slots
-- ================================================================================
GO
    CREATE PROCEDURE uspAddSlot

    AS
        SET NOCOUNT ON;
        SET XACT_ABORT ON;

        BEGIN TRANSACTION
            -- Incriment the Identity Column adding a new slot
            INSERT INTO [HeadCount_Ver01].[dbo].Slots DEFAULT VALUES;
            -- Return the SlotID
            RETURN(SELECT COALESCE(MAX(SlotID),1) FROM [HeadCount_Ver01].[dbo].Slots);
        COMMIT;
GO


-- ================================================================================
-- Add Department Slots
-- ================================================================================
GO
    CREATE PROCEDURE uspAddDepartmentSlots
        @departmentID       INTEGER,
        @SlotID             INTEGER
    AS
        SET NOCOUNT ON;
        SET XACT_ABORT ON;

        BEGIN TRANSACTION
            -- We are Assigning a Slot to a Department but we aren't Assigning any one to it
            INSERT INTO [HeadCount_Ver01].[dbo].DepartmentSlots
            VALUES (@departmentID, @SlotID, NULL, NULL, NULL);
        COMMIT;
GO


-- ================================================================================
-- Generate Slots
-- ================================================================================
GO
    CREATE PROCEDURE uspGenerateSlots
        @departmentID       INTEGER,
        @allocatedSlots     INTEGER
    AS
        SET NOCOUNT ON;
        SET XACT_ABORT ON;

        BEGIN TRANSACTION
            DECLARE @i      INTEGER = 0;
            DECLARE @SlotID INTEGER = 0;
            WHILE @i < @allocatedSlots
                BEGIN
                    EXECUTE @SlotID = uspAddSlot;
                    EXECUTE uspAddDepartmentSlots @departmentID, @SlotID;
                    SET @i += 1;
                END
        COMMIT;
GO

它完全按照预期的方式运行,除了错误“ EXECUTE之后的事务计数指示BEGIN和COMMIT语句的数量不匹配”会在调用应用程序尝试执行此存储过程时导致异常。

0 个答案:

没有答案