on ‎2023 Dec 15 11:47 AM
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
Request clarification before answering.
I would debug this with the Stored Procedure debugger in SQL Central.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 12 | |
| 9 | |
| 7 | |
| 5 | |
| 4 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.