如何在SQL Server触发器中识别操作类型(插入,更新,删除)

时间:2014-09-25 07:33:13

标签: sql sql-server triggers

我们在SQL Server中使用以下触发器来维护历史记录,我现在需要识别操作,就像插入,更新或删除一样。我找到了一些信息HERE,但它不适用于SQL Server。

CREATE TRIGGER audit_guest_details ON [PMS].[GSDTLTBL] 
FOR INSERT,UPDATE,DELETE
AS
    DECLARE @SRLNUB1 INT;
    DECLARE @UPDFLG1 DECIMAL(3,0);

    SELECT @SRLNUB1 = I.SRLNUB FROM inserted I;
    SELECT @UPDFLG1 = I.UPDFLG FROM inserted I;   

    BEGIN
       /* Here I need to identify the operation and insert the operation type in the GUEST_ADT 3rd field */
       insert into dbo.GUEST_ADT values(@SRLNUB1,@UPDFLG1,?);

       PRINT 'BEFORE INSERT trigger fired.'
    END;
GO

但在这里我需要识别操作并希望相应地插入操作类型。

这里我不想为每个操作创建三个触发器

4 个答案:

答案 0 :(得分:1)

For Inserted:仅插入行。 对于更新:插入和删除行。 对于已删除:仅删除行。

DECLARE @event_type varchar(42)
IF EXISTS(SELECT * FROM inserted)
  IF EXISTS(SELECT * FROM deleted)
    SELECT @event_type = 'update'
ELSE
    SELECT @event_type = 'insert'
ELSE
  IF EXISTS(SELECT * FROM deleted)
    SELECT @event_type = 'delete'
  ELSE
    --no rows affected - cannot determine event
    SELECT @event_type = 'unknown'

答案 1 :(得分:1)

这是Mikhail使用searched CASE expression的答案的简化版本。

DECLARE @Operation varchar(7) = 
    CASE WHEN EXISTS(SELECT * FROM inserted) AND EXISTS(SELECT * FROM deleted) 
        THEN 'Update'
    WHEN EXISTS(SELECT * FROM inserted) 
        THEN 'Insert'
    WHEN EXISTS(SELECT * FROM deleted)
        THEN 'Delete'
    ELSE 
        NULL --Unknown
    END;

答案 2 :(得分:0)

create trigger my_trigger on my_table
after update , delete , insert
as
declare @inserting bit
declare @deleting  bit
declare @updating  bit = 0

select @inserting = coalesce (max(1),0) where exists (select 1 from inserted)
select @deleting  = coalesce (max(1),0) where exists (select 1 from deleted )
select @inserting = 0
     , @deleting = 0
     , @updating = 1
  where @inserting = 1 and @deleting = 1
print   'Inserting = ' + ltrim (@inserting)
    + ', Deleting = '  + ltrim (@deleting)
    + ', Updating = '  + ltrim (@updating)

如果所有三个都为零,则没有行受到影响,我认为没有办法判断它是否是更新/删除/插入。

答案 3 :(得分:0)

由于您可以一次获取多行,我们按如下方式进行。

paths:
  /getDistance/{startAndEndCoords}:
    get:
      tags: 
        - get distance
        - get distance in kilometers
      summary: get the distance between two GPS coordinates in Kilometers.
      description:  > 
        startAndEndCoords represents the start and end GPS coordinates in the following order longitude and latitude respectively. Passing the start and end
        GPS cootdinates in the URL will result in invoking and communicating with OpenRouteService. The latter service will send a response in a form of json
        file.The JSON file will be fetched for the distance. The distance fetched, is the distance between the GPS coordinates entered and it is in meters, however
        they are displayed to the users in Kilometers.
      operationId:  getDistance
      parameters:
        - name: startAndEndCoords
          in: path
          description: Start and end GPS coordinates as comma separated values in terms of longitude and latitude respectively. For example 
            12.497366,41.922782,-3.606997572,40.47416477
          required: true
          schema:
            type: number
            format: float
          allowEmptyValue: false
      responses:
        '200':
          description: Distance fetched successfully.