-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には複数の行を含めることができるため、SELECT代わりに a を使用してください。

あなたの場合:

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複数の行を含めることができます。そのため、その中の列からスカラー変数に値を代入することは常に間違いです。

代わりに試してください:

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現在のクエリのこれらの各s は、任意の行から値を選択します。各 s が同じ任意の行から値を取得することdeletedは保証されていません。SELECT

于 2013-06-25T12:24:33.417 に答える