在多个IF NOT EXISTS的情况下使用Merge语句

时间:2017-06-10 03:11:45

标签: sql sql-server

我有以下存储过程,它使用多个IF NOT EXISTS条件

Create procedure AuditLogProcDetails
(@Id                 int,
 @LogId          int,
 @LogDescription varchar(2000),
 @LogStartDate   datetime     ,
 @LogEndDate     datetime     ,
 @Task               varchar(100)   
)
as 
BEGIN
  IF NOT EXISTS (SELECT * FROM dbo.AuditLogging where LogId = @LogId and Task = @Task )
    begin
      INSERT INTO dbo.AuditLogging( Id, LogId, Task, LogDescription, StartTime       , EndTime)
                             VALUES (@Id,@LogId,@Task,@LogDescription,@LogStartDate, @LogStartDate)
    end
  ELSE
    begin
      UPDATE dbo.AuditLogging
         SET LogDescription = @LogDescription,
             StartTime = @LogStartDate,
             EndTime=    @LogEndDate 
       WHERE ProcessId = @LogId
         AND Task      = @Task

    end
  -- COMMIT;

    IF NOT EXISTS (SELECT * FROM dbo.AuditLogging where LogId = @LogId and Task = 'INPROGRESS' )
        DECLARE @SetStartTimeInProgress datetime;
        DECLARE @TotalCount int;
    begin

        SET @SetStartTimeInProgress = GETDATE();
      UPDATE dbo.AuditLogging
         SET StartTime = @LogStartDate,
             EndTime=    @LogEndDate 
       WHERE LogId = @LogId
         AND Task      = 'INPROGRESS'        
    end

    IF NOT EXISTS (SELECT * FROM dbo.AuditLogging where LogId = @LogId and Task = 'COMPLETED' )
        DECLARE @SetStartTimeCompleted datetime;
        DECLARE @TotalCount int;
    begin

        SET @SetStartTimeCompleted = LastRunStartDate FROM LogTable  WHERE LogId=@LogId and Status='COMPLETED';
      UPDATE dbo.AuditLogging
         SET StartTime = @SetStartTimeCompleted,
             TOtalCount=    @TotalCount 
       WHERE LogId = @LogId
         AND Task      = 'COMPLETED'         
    end

END

如何重构存储过程代码以上并使用更有效的MERGE语句代替IF NOT EXISTS

1 个答案:

答案 0 :(得分:1)

我认为你可以这样做:

CREATE PROCEDURE AuditLogProcDetails
(
    @Id             int,
    @LogId          int,
    @LogDescription varchar(2000),
    @LogStartDate   datetime,
    @LogEndDate     datetime,
    @Task           varchar(100)   
)
AS
BEGIN
  IF NOT EXISTS 
  (
      SELECT * 
      FROM dbo.AuditLogging 
      WHERE LogId = @LogId 
      AND Task = @Task 
  )
  BEGIN
      INSERT INTO dbo.AuditLogging(Id, LogId, Task, LogDescription, StartTime, EndTime)
      VALUES (@Id, @LogId, @Task, @LogDescription, @LogStartDate, @LogStartDate)
  END
  ELSE
  BEGIN
      UPDATE dbo.AuditLogging
      SET    LogDescription = @LogDescription,
             StartTime = @LogStartDate,
             EndTime=    @LogEndDate 
      WHERE  ProcessId = @LogId
      AND    Task      = @Task

  END

  DECLARE @SetStartTimeInProgress datetime;
  DECLARE @TotalCount int;

  SET @SetStartTimeInProgress = GETDATE();

  UPDATE dbo.AuditLogging
  SET StartTime = @LogStartDate,
      EndTime   = @LogEndDate 
  WHERE LogId   = @LogId
  AND Task = 'INPROGRESS'        

  SELECT @SetStartTimeCompleted = LastRunStartDate 
  FROM LogTable  
  WHERE LogId = @LogId 
  AND [Status]  = 'COMPLETED';

  UPDATE dbo.AuditLogging
  SET StartTime  = @SetStartTimeCompleted,
      TOtalCount = @TotalCount 
  WHERE LogId = @LogId
  AND Task    = 'COMPLETED'            
END