【问题标题】:AFTER DELETE Trigger is not inserting multiple rows [closed]AFTER DELETE 触发器未插入多行 [关闭]
【发布时间】:2013-06-25 12:17:37
【问题描述】:
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

【问题讨论】:

  • 这是什么数据库系统?触发器没有标准化,sql 标记用于标准 SQL 语言。另外,既然您已经编写了一个部分工作的触发器,那么代码在哪里?
  • 向我们展示您的触发器。 :)
  • 如果您发布当前代码,我们或许可以修复它。
  • 什么样的行?网格行? HTML表格行? SQL 行?
  • @jlafay sql 被标记。 ;)

标签: sql sql-server


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    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;
    

    但到目前为止,这种情况很少见——我想不出任何想法。

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

    【讨论】:

    • 谢谢你,先生。它的工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    相关资源
    最近更新 更多