-6
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
4

2 回答 2

4

CREATE TRIGGER

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

DELETED可以包含多行,因此请改用 a 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
于 2013-06-25T12:20:49.680 回答
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;

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

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

于 2013-06-25T12:24:33.417 回答