cancel
Showing results for 
Search instead for 
Did you mean: 

Please help me check trigger error

ximen
Participant
0 Kudos
437

When the updated value contains a comma, the trigger inserts the original column value,

**

CREATE TABLE "DBA"."test_split" (
    "UUID" INTEGER NOT NULL DEFAULT AUTOINCREMENT,
    "goods" VARCHAR(90) NULL,
    "price" DECIMAL(111,3) NULL,
    "OLD_uuid" INTEGER NULL,
    CONSTRAINT "ID" PRIMARY KEY ( "UUID" ASC )
) IN "system";
COMMENT ON TABLE "DBA"."test_split" IS '
';
--insert demo data
INSERT INTO "DBA"."test_split" ("UUID","goods","price","OLD_uuid") VALUES(1,'apple',10.000,NULL);
--create trigger where update 
CREATE TRIGGER "update_trigger" AFTER UPDATE OF "goods"
ORDER 1 ON "DBA"."test_split"
  REFERENCING OLD AS old_test_split NEW AS new_test_split
FOR EACH ROW  WHEN(  CHARINDEX(',', new_test_split.goods)>0)   -- Including commas to trigger business execution
BEGIN
     declare @old_uuid INTEGER ;
     declare @old_goods varchar(100);
     declare @old_price  int;
     declare @instert_new_goods varchar(100);
     select  uuid,goods,price into @old_uuid,@old_goods,@old_price  from test_split where Old_test_split.uuid=test_split.uuid ;
  select right(goods,len(goods)-CHARINDEX(',',goods)) into @instert_new_goods from  test_split where new_test_split.uuid=test_split.uuid; 
  --Replace Update
update  test_split set goods=@instert_new_goods from  test_split where new_test_split.uuid=test_split.uuid; 
--Reinsert old data and record uuid
INSERT INTO "DBA"."test_split" ("goods","price","OLD_uuid") 
VALUES(@old_goods,@old_price,@old_uuid)
END;

**

--run:

update test_split set goods='apple,test',price=20 where uuid=1;

UUID   goods     price    old_uuid
1      test       20        
2     apple       10      1

Accepted Solutions (1)

Accepted Solutions (1)

chris_keating
Product and Topic Expert
Product and Topic Expert

I would debug this with the Stored Procedure debugger in SQL Central.

  • Navigate to the trigger source in SQL Central
  • Select Mode | Debug
  • Set a breakpoint after the first select
  • Execute the update

Examine the value set for the "@old__goods_". It is the values currently in the table - the update has completed by the time this trigger is fired. You should access the old values using Old_test_split.

Answers (0)