sql – 是否有必要在此代码中放置事务?

我的主管让我不要在这段代码中放置交易和提交等,因为他说在这个程序中放置交易是没用的.他经验丰富,我不能直接与他争辩,所以需要你的观点吗?

ALTER PROCEDURE [Employee].[usp_InsertEmployeeAdvances](
      @AdvanceID        BIGINT,
      @Employee_ID      INT,
      @AdvanceDate      DATETIME,
      @Amount           MONEY,
      @MonthlyDeduction MONEY,
      @Balance          MONEY,
      @SYSTEMUSER_ID    INT,
      @EntryDateTime    DATETIME = NULL,
      @ProcedureType    SMALLINT)
AS
BEGIN
    BEGIN TRY
        BEGIN TRANSACTION [Trans1]
        IF EXISTS
           (
              SELECT *
              FROM Employee.Advance
              WHERE AdvanceID = @AdvanceID
           )
        BEGIN
            --UPDATION OF THE RECORD
            IF @ProcedureType = 1
            BEGIN
                SET @Amount = @Amount * -1;
            END
            UPDATE Employee.Advance
            SET
                  Employee_ID = @Employee_ID,
                  AdvanceDate = @AdvanceDate,
                  Amount = @Amount,
                  MonthlyDeduction = @MonthlyDeduction,
                  Balance = @Balance,
                  EntryDateTime = GETDATE()
            WHERE AdvanceID = @AdvanceID
        END
        ELSE
        BEGIN
            DECLARE @LastRecordID INT
            DECLARE @LastBalance MONEY
            SET @LastRecordID =
               (
                  SELECT MAX(EA.AdvanceID)
                  FROM Employee.Advance EA
                  WHERE EA.Employee_ID = @Employee_ID
               )
            SET @LastBalance =
               (
                  SELECT EA.Balance
                  FROM Employee.Advance EA
                  WHERE EA.AdvanceID = ISNULL(@LastRecordID, 0)
               )
            IF(@ProcedureType = 0) --Advances
            BEGIN
                SET @Balance = ISNULL(@LastBalance, 0) + @Amount
                INSERT INTO Employee.Advance
                                            (Employee_ID,
                                             AdvanceDate,
                                             Amount,
                                             MonthlyDeduction,
                                             Balance,
                                             User_ID,
                                             EntryDateTime
                                            )
                VALUES
                       (@Employee_ID,
                        @AdvanceDate,
                        @Amount,
                        @MonthlyDeduction,
                        @Balance,
                        @SYSTEMUSER_ID,
                        GETDATE())
            END
            ELSE --Receivings
            BEGIN
                IF NOT EXISTS
                   (
                      SELECT *
                      FROM Employee.Advance EA
                      WHERE EA.Employee_ID = @Employee_ID
                            AND EA.Balance > 0
                            AND EA.AdvanceID =
                         (
                            SELECT MAX(AdvanceID)
                            FROM Advance
                            WHERE Employee_ID = @Employee_ID
                         )
                   )
                BEGIN
                    RAISERROR('This Employee has no advances history', 16, 1)
                    RETURN
                    --Select 0                  
                END
                ELSE
                BEGIN
                    SET @Balance = ISNULL(@LastBalance, 0) - @Amount
                    INSERT INTO Employee.Advance
                                                (Employee_ID,
                                                 AdvanceDate,
                                                 Amount,
                                                 MonthlyDeduction,
                                                 Balance,
                                                 User_ID,
                                                 EntryDateTime
                                                )
                    VALUES
                           (@Employee_ID,
                            @AdvanceDate,
                            -1 * @Amount,
                            @MonthlyDeduction,
                            @Balance,
                            @SYSTEMUSER_ID,
                            GETDATE())
                END
            END
        END
        COMMIT TRANSACTION [Trans1]
    END TRY
    BEGIN CATCH
        ROLLBACK TRANSACTION [Trans1]
    END CATCH
END

最佳答案

ALTER PROCEDURE [Employee].[usp_InsertEmployeeAdvances]
(
      @AdvanceID        BIGINT,
      @Employee_ID      INT,
      @AdvanceDate      DATETIME,
      @Amount           MONEY,
      @MonthlyDeduction MONEY,
      @Balance          MONEY,
      @SYSTEMUSER_ID    INT,
      @EntryDateTime    DATETIME = NULL,
      @ProcedureType    SMALLINT
)
AS BEGIN

    SET NOCOUNT ON

    IF EXISTS (
        SELECT 1
        FROM Employee.Advance
        WHERE AdvanceID = @AdvanceID
    )
    BEGIN

        UPDATE Employee.Advance
        SET
                Employee_ID = @Employee_ID,
                AdvanceDate = @AdvanceDate,
                Amount = CASE WHEN @ProcedureType = 1 THEN -@Amount ELSE @Amount END,
                MonthlyDeduction = @MonthlyDeduction,
                Balance = @Balance,
                EntryDateTime = GETDATE()
        WHERE AdvanceID = @AdvanceID

    END
    ELSE BEGIN

        DECLARE
              @LastRecordID INT
            , @LastBalance MONEY
            , @IsBalance BIT

        SELECT @LastRecordID = MAX(AdvanceID)
        FROM Employee.Advance
        WHERE Employee_ID = @Employee_ID

        SELECT
            @LastBalance = Balance,
            @IsBalance = CASE WHEN Balance > 0 THEN 1 ELSE 0 END
        FROM Employee.Advance
        WHERE AdvanceID = ISNULL(@LastRecordID, 0)

        IF ISNULL(@IsBalance, 0) = 0 BEGIN

            RAISERROR('This Employee has no advances history', 16, 1)
            RETURN 

        END
        ELSE BEGIN

            INSERT INTO Employee.Advance(Employee_ID, AdvanceDate, Amount, MonthlyDeduction, Balance, [User_ID], EntryDateTime)
            SELECT
                @Employee_ID,
                @AdvanceDate,
                CASE WHEN @ProcedureType = 0 THEN @Amount ELSE -@Amount END,
                @MonthlyDeduction,
                ISNULL(@LastBalance, 0) + CASE WHEN @ProcedureType = 0 THEN @Amount ELSE -@Amount END,
                @SYSTEMUSER_ID,
                GETDATE()

        END

    END

END
点赞