I am trying to write a trigger in SAP HANA to update a field of a table when new records are being inserted to that table. Following is a sample trigger that I have written.
CREATE TRIGGER SAMPLE
AFTER INSERT ON TARGET_TABLE
REFERENCING NEW ROW NEW_ROW
FOR EACH ROW
BEGIN
UPDATE TARGET_TABLE SET VALID_FROM='2018-02-01' WHERE ITEM=:NEW_ROW.ITEM
END
When I try this, I get the error:
Modification of subject table in trigger not allowed
Is there a way by which I can achieve this?
This suggests to use the transition variable NEW_ROW, appreciate if a code sample can be provided.
You don't actually need to create trigger for your requirement as I can see from your post (of course if it is only updating a date field)
You can define the VALID_FROM column with a DEFAULT value
For example,
Create Column Table DefaultColumnTable (
Id int,
Code varchar(5),
VALID_FROM date default '2018-02-01'
)
So whenever a new row is inserted, unless an alternative value is stated the valid_from column will be populated with default date specified in the DDL command above.
The users can change the valid_from field value without any problem
#Kalpa, maybe you can use BEFORE INSERT Trigger
Please check following sample
create trigger TriggerTable_B_INS BEFORE INSERT on TriggerTable
REFERENCING NEW ROW mynewrow
FOR EACH ROW
begin
declare lv_d date;
lv_d := '20180201';
mynewrow.VALID_FROM = :lv_d;
end;
You set only the new row columns before Insert command executes over the target table.
You don't explicitly execute an INSERT command, just set new values for new row columns. That's all
I hope it helps
Related
I trying to write a trigger that will update a column when user insert or updates a row, within the same table.
Example:
insert into user(ID, F_NM, L_NM, EMAIL) values ('1', 'John','Doe','john.doe#market.org.com');
after the insert, i want to call: update user set ORG = 'market' where ID = '1'.
create or replace trigger user_change
after insert or update of EMAIL on USER
for each row
declare
NEW_ORG VARCHAR(10);
BEGIN
CASE
when :NEW.EMAIL like '$#market.org.com' then
NEW_ORG := 'market';
........
END CASE;
UPDATE USER set ORG = NEW_ORG where ID = :NEW.ID
END;
Calculating the new ORG work, but I can't get the update statement to work.
I get 'ORA-04091 table USER is mutating, trigger/funtion may not see it', figure its due to me inserting/updating the same record at same time. Tried adding 'pragma autonomous_transaction' and 'commit' to the trigger, the insert/update of fields works but the ORG does not get updated.
Also tried changing to INSTEAD OF INSERT OR UPDATE OF EMAIL but I keep getting 'ORA-04073 column list not valid for this trigger type'
create or replace trigger user_change
instead of insert or update of EMAIL on USER
while i get 'ORA-25002 cannot create instead of triggers on tables'
create or replace trigger user_change
instead of insert on USER
Why not simply turn the trigger to a before trigger, when you can set the value before it is written? This way, you don't need to run a new DML statement on the table, which avoid the "mutating" error.
create or replace trigger user_change
after insert or update of email on user
for each row
begin
if :new.email like '%#market.org.com' then
:new.org := 'market';
end if;
end;
Looks like your org column can be calculated virtual column. In this case it would be better to create user-defined deterministic pl/sql function that returns correct calculated value and add it to your table, for example:
Alter table t add org varchar2(30) generated always as (f_get_org(email))
I am trying to make an upsert trigger on ORACLE via PL/SQL by checking some examples, i am doing fine, i think it is the last step i should only configure. My requirement is that :
A system that will insert to that field will remain one column always null, so i will read column value from another table, then upsert it with inclusion of that value.
d2c_region_locale_config holds d2c_is_active value, so i firstly read that value regarding to locale condition then trigger inserts or updates to table with addition of this value on active_for_d2c column.(for update i am using locale and country columns as it is shown on where clause, they are not PK but has not null condition)
So i've created this trigger:
CREATE OR REPLACE TRIGGER BL_PIM_LOCALE_COUNTRY
BEFORE INSERT OR UPDATE ON PIM_LOCALE_COUNTRY REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
l_active_for_d2c INTEGER;
BEGIN
if :NEW.active_for_d2c is null then
DELETE from pim_locale_country where active_for_d2c is null;
select distinct(d2c_isactive) into l_active_for_d2c from d2c_region_locale_config where d2c_locale= :NEW.locale;
UPDATE pim_locale_country
SET locale = :NEW.locale, locale_name = :NEW.locale_name,
country = :NEW.country, country_name = :NEW.country_name, isdummy = :NEW.isdummy,
active_for_d2c = l_active_for_d2c, itextpos = :NEW.itextpos, locale_charset = :NEW.locale_charset,
fallback_locale = :NEW.fallback_locale, default_for_lang = :NEW.default_for_lang, opeclang = :NEW.opeclang
where locale = :NEW.locale and country = :NEW.country;
IF ( sql%notfound ) THEN
INSERT INTO PIM_LOCALE_COUNTRY (locale,locale_name,country,country_name,isdummy,active_for_d2c,itextpos,locale_charset,fallback_locale,default_for_lang,opeclang)
VALUES (:NEW.locale, :NEW.locale_name,:NEW.country,:NEW.country_name,:NEW.isdummy,l_active_for_d2c,:NEW.itextpos,:NEW.locale_charset,:NEW.fallback_locale,:NEW.default_for_lang,:NEW.opeclang);
END IF;
end if;
END;
It currently does the job, reads value and inserts or updates the existing locale-country couple for other values. But critical thing is that, table always has one "null" value(Please check screenshot), even that i run delete statement at the beginning on my trigger. So my question would be how to delete, or how to make this approach on trigger side ?
Many thanks for answers!
Trigger before insert doesn't block insert itself, so you insert that record twice. That is, once your trigger done its work (inserted or updated record), oracle will proceed with insert (or update) using values that stand in NEW record of your trigger. If trigger modifies NEW., it will be stored as you changed it, but if trigger inserts something itself, you can get more records.
You can use instead of insert or update triggers, and then oracle will not run its own inserts/updates after trigger finishes.
But more common way for 1-record triggers is to modify fields in NEW, for this case field NEW.d2c_is_active.
It looks like this (possible typos, please check)
CREATE OR REPLACE TRIGGER BL_PIM_LOCALE_COUNTRY
BEFORE INSERT OR UPDATE ON PIM_LOCALE_COUNTRY REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
if :NEW.active_for_d2c is null then
select d2c_isactive
into :NEW.active_for_d2c
from d2c_region_locale_config
where d2c_locale= :NEW.locale and rownum<=1;
end if;
END;
I have a table in Oracle with Timestamp field, a key field and 2 other columns. Am looking to create a trigger which would update another table with just the current state row data based on key, timestamp. Am new to trigger, any inputs and / or recommendation would be highly appreciated.
create or replace TRIGGER A
AFTER INSERT
ON A1
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
DECLARE
BEGIN
UPDATE C SET COLC=:new.colc, D=:new.cold, TIMESTAMP=:new.timestamp WHERE KEY=:new.key;
EXCEPTION
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END A;
So, I understand how to create a trigger in HANA, but the examples in the HANA reference material do not mention how a trigger on Table A can update Table A; instead the material is always trigger on A updates B like the following example from the SAP docs:
CREATE TRIGGER TEST_TRIGGER_VAR_UPDATE
AFTER UPDATE ON TARGET
REFERENCING NEW ROW mynewrow, OLD ROW myoldrow
FOR EACH ROW
BEGIN
INSERT INTO SAMPLE_new VALUES(:mynewrow.a, :mynewrow.b);
INSERT INTO SAMPLE_old VALUES(:myoldrow.a, :myoldrow.b);
INSERT INTO SAMPLE VALUES(0, 'trigger');
END;
What's the preferred way of updating a column such as a LastModifiedDate? Would I set :mynewrow.LastModifiedDate = NOW(), or should I perform an UPDATE where the ID matches the ID of mynewrow?
As of SPS8, a HANA SQL Trigger can only perform INSERT, UPDATE, and DELETE statements against other tables. If you try to create a self-referencing Trigger, you'll find the following error message:
Modification of subject table in trigger not allowed
I have a table with a trigger applied to it, that populates an audit table if it changes. I created the original table with just two fields:
CREATE TABLE events(
code serial8 NOT NULL,
event date NOT NULL,
);
And I want a trigger to populate the audit table whenever the event date on this first table is updated:
CREATE TABLE audit(
date_log date,
time_log time,
userid char(20),
event_code int,
action_log text,
old_date date,
new_date date
);
The trigger function:
CREATE OR REPLACE FUNCTION auditing() RETURNS trigger AS $$ BEGIN
INSERT INTO audit(date_log,time_log,userid,event_code,action_log,old_date,new_date)
SELECT current_date,current_time,current_user,code,tg_op,OLD.event, NEW.event FROM events;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
And I set the trigger like this:
CREATE TRIGGER audit_dates
AFTER UPDATE OF event ON events
FOR EACH ROW EXECUTE PROCEDURE auditing();
The problem I am having is that whenever I run an update function such as:
UPDATE events SET event = '2013-03-01' WHERE code = 1;
The original table updates just fine, but the table that should audit this, generates one audit row for each row that the original table had, so if the original table has 200 rows and I update just one of them, 200 new rows are generated in the audit table.
I am trying to think whether this is because I created the trigger as FOR EACH ROW , but at the same time I have been reading the docs on this and it looks like this is the syntax I need.
What is the right way to get just one new row in the audit table for each time that I update the original table?
You have SELECT ... FROM events in the trigger, so it's selecting all events. You can just use a simple INSERT ... VALUES since NEW and OLD are variables.