AFTER DELETE触发器未插入多行

时间:2013-06-25 12:17:37

标签: sql sql-server

ALTER 
TRIGGER [tgr_del_into_plan_audit]
ON [dbo].[agent_plan_details]
AFTER DELETE
AS 

    declare @plan_title_id int;
    declare @plan_title varchar(50);
    declare @no_of_property_listing int;
    declare @validity_of_plan int;
    declare @Each_property_listing_life int;
    declare @list_of_property_in_search_result int;
    declare @price decimal(8,2);
    declare @discount BIT;
    declare @discount_code varchar(10);
    declare @discount_percentage int

    select @plan_title_id=d.plan_title_id from deleted d;
    select @plan_title=d.plan_title from deleted d;
    select @no_of_property_listing=d.no_of_property_listing from deleted d;
    select @validity_of_plan=d.validity_of_plan from deleted d;
    select @Each_property_listing_life=d.Each_property_listing_life from deleted d;
    select @list_of_property_in_search_result=d.list_of_property_in_search_result from deleted d;
    select @price=d.price from deleted d;
    select @discount=d.discount from deleted d;
    select @discount_code=d.discount_code from deleted d;
    select @discount_percentage=d.discount_percentage from deleted d;

BEGIN

SET NOCOUNT ON

INSERT INTO agent_plan_details_audit 
                    ( plan_title_id,
                     plan_title, 
                     no_of_property_listing,
                     validity_of_plan,
                     Each_property_listing_life,
                     list_of_property_in_search_result,
                     price,
                     discount,
                     discount_code,
                     discount_percentage,
                     create_date,
                     action )

                VALUES
                    (@plan_title_id,
                    @plan_title,
                    @no_of_property_listing,
                    @validity_of_plan,
                    @Each_property_listing_life,
                    @list_of_property_in_search_result,
                    @price,
                    @discount,
                    @discount_code,

        @discount_percentage,
                    GETDATE(),
                     'D')




END

2 个答案:

答案 0 :(得分:4)

请参阅CREATE TRIGGER

CREATE TRIGGER TRIG_SomeTable_Delete
    ON SomeTable AFTER DELETE
    AS
        BEGIN
            INSERT INTO SomeTable(SomeColumn)
            SELECT SomeColumn FROM DELETED
        END

DELETED可以包含多行,因此请改用SELECT

在你的情况下:

ALTER TRIGGER [tgr_del_into_plan_audit]
    ON [dbo].[agent_plan_details]
    AFTER DELETE
    AS
       BEGIN
           SET NOCOUNT ON;
           INSERT INTO agent_plan_details_audit
           (
               plan_title_id,
               plan_title,
               no_of_property_listing,
               validity_of_plan,
               Each_property_listing_life,
               list_of_property_in_search_result,
               price,
               discount,
               discount_code,
               discount_percentage,
               create_date,
               action
           )
           SELECT
               plan_title_id,
               plan_title,
               no_of_property_listing,
               validity_of_plan,
               Each_property_listing_life,
               list_of_property_in_search_result,
               price,
               discount,
               discount_code,
               discount_percentage,
               GETDATE(),
               'D'
           FROM DELETED
       END

答案 1 :(得分:3)

deleted可以包含多个行。因此,将值中的列赋值给标量变量总是 1 是一个错误。

请尝试:

ALTER 
TRIGGER [tgr_del_into_plan_audit]
ON [dbo].[agent_plan_details]
AFTER DELETE
AS 

SET NOCOUNT ON

INSERT INTO agent_plan_details_audit 
( plan_title_id,
  plan_title, 
  no_of_property_listing,
  validity_of_plan,
  Each_property_listing_life,
  list_of_property_in_search_result,
  price,
  discount,
  discount_code,
  discount_percentage,
  create_date,
  action )
SELECT
  plan_title_id,
  plan_title, 
  no_of_property_listing,
  validity_of_plan,
  Each_property_listing_life,
  list_of_property_in_search_result,
  price,
  discount,
  discount_code,
  discount_percentage,
  GETDATE(),
  'D'
FROM
  deleted

1 有人必须指出某些边缘情况,不是错误在触发器中有这样的语句:

select @plan_title_id=d.plan_title_id from deleted d;

但是这种情况到目前为止还是少数 - 我无法想到任何问题。

当前查询中的每个SELECT都将从deleted任意行中选择一个值 - 甚至不能保证每个SELECT s将从相同的任意行获取一个值。