SQL Insert Trigger - sql

I have a table ARCHIVED_TIMESTAMP with columns ID INT, ID_ELEMENT REFERENCES ELEMENT(ID) and ARCHIVED_TIMESTAMP TIMESTAMP
I want to create a trigger that automatically inserts in the table ARCHIVED_TIMESTAMP after every insert in the ELEMENT table the id of the inserted element (ID_ELEMENT=ID) and the timestamp from the insertion(ARCHIVED_TIMESTAMP=CURRENT_TIMESTAMP)

If I understood correctly then please try something like this:
CREATE TRIGGER TRG_ELEMENT_FOR_INS ON ELEMENT
FOR INSERT
AS
BEGIN
INSERT INTO ARCHIVED_TIMESTAMP(ID_ELEMENT, ARCHIVED_TIMESTAMP)
SELECT INS.ID
, INS.CURRENT_TIMESTAMP
FROM Inserted INS
END -- End trigger TRG_ELEMENT_FOR_INS

Related

SQL Server: If a row is inserted in one table, how do I write a trigger to insert that same row in a different table?

I'm not sure how to create this trigger,.I need to add a row in pub_info table when a row is inserted in publishers table. The exact same row. SQL server
CREATE TRIGGER checkCity
ON pub_info
AFTER INSERT
AS
IF -- a row is inserted into publishers table,
-- how do I add the same row into pub_info table?
INSERT VALUES(#pub_id, NULL, 'new publishers')
BEGIN
END;
You have to create a trigger on publishers table and not on pub_info .
The inserted data is available in the INSERTED table in the trigger
CREATE TRIGGER checkCity
ON publishers
AFTER INSERT
AS
BEGIN
INSERT INTO pub_info(pubid, pubname, pub_description)
SELECT pubid, pubname, pub_description
FROM INSERTED;
END;

INSERT TRIGGER AFTER UPDATE ANOTHER TABLE

There are 2 tables orders and pizza. I have to create trigger update_order_pizza which will insert row in table order_pizza after new row is inserted in table order ( or when row is updated).
Code under trigger work when I launch in SQL, but I don't see what changes in table order_pizza after I inserted row in order.
CREATE DEFINER=`root`#`localhost` TRIGGER `update_order_pizza` AFTER INSERT ON `orders` FOR EACH ROW BEGIN
set #orderid = (select max(order_id) from orders);
set #pizzaid = (select max(pizza_id) from pizza);
insert into order_pizza(order_id,pizza_id)
values(#orderid,#pizzaid);
END
I expect to see next
If I insert new order , let;s say order id=36 in table order_pizza should be inserted new record (36,64)
Your code should be using the new variable to reference rows. In addition, your tables should be defining the ids as auto-increment.
So, the code should look more like this:
create trigger `update_order_pizza` after update on `orders`
for each row
begin
insert into order_pizza (order_id)
values (new.order_id);
end;
Alternatively, the pizza_id might be in the orders table, and you may intend:
insert into order_pizza (order_id, pizza_id)
values (new.order_id, new.pizza_id);

Values of the inserted row in a Trigger Oracle

I want a trigger that updates the value of a column, but I just want to update a small set of rows that depends of the values of the inserted row.
My trigger is:
CREATE OR REPLACE TRIGGER example
AFTER INSERT ON table1
FOR EACH ROW
BEGIN
UPDATE table1 t
SET column2 = 3
WHERE t.column1 = :new.column1;
END;
/
But as I using FOR EACH ROW I have a problem when I try it, I get the mutating table runtime error.
Other option is not to set the FOR EACH ROW, but if I do this, I dont know the inserted "column1" for comparing (or I dont know how to known it).
What can I do for UPDATING a set of rows that depends of the last inserted row?
I am using Oracle 9.
You should avoid the DML statements on the same table as defined in a trigger. Use before DML to change values of the current table.
create or replace trigger example
before insert on table1
for each row
begin
:new.column2 := 3;
end;
/
You can modify the same table with pragma autonomous_transaction:
create or replace trigger example
after insert on table1 for each row
declare
procedure setValues(key number) is
pragma autonomous_transaction;
begin
update table1 t
set column2 = 3
where t.column1 = key
;
end setValues;
begin
setValues(:new.column1);
end;
/
But I suggest you follow #GordonLinoff answere to your question - it's a bad idea to modify the same table in the trigger body.
See also here
If you need to update multiple rows in table1 when you are updating one row, then you would seem to have a problem with the data model.
This need suggests that you need a separate table with one row per column1. You can then fetch the value in that table using join. The trigger will then be updating another table, so there will be no mutation problem.
`create table A
(
a INTEGER,
b CHAR(10)
);
create table B
(
b CHAR (10),
d INTEGER
);
create trigger trig1
AFTER INSERT ON A
REFERENCING NEW AS newROW
FOR EACH ROW
when(newROW.a<=10)
BEGIN
INSERT into B values(:newROW.b,:newROW.a);
END trig1;
insert into A values(11,'Gananjay');
insert into A values(5,'Hritik');
select * from A;
select * from B;`

How to use SQL TRIGGER to insert rows from another table into a new one?

This is my create trigger statement:
CREATE OR REPLACE TRIGGER time_of_inserts
AFTER INSERT ON t_workers
FOR EACH ROW
BEGIN
SELECT pk_workerid, sysdate archivetime
into t_logtable
from t_workers
END;
What I want is, every time when I insert a new row into the t_workers table, I want to insert a new primary key (it could be unique from 1, or copying the inserted data), and the time of the insert into a new table (T_logTable). But I don't know how to insert this into a new table every time. You can see above, what I've came up so far.
"my main problem is inserting into a new table, every time it's triggered"
But you're not inserting, you're selecting. To insert you need to specify the insert keyword :)
Also, you don't want to select from the triggering table: use the :NEW namespace instead.
CREATE OR REPLACE TRIGGER time_of_inserts
AFTER INSERT ON t_workers
FOR EACH ROW
BEGIN
insert into t_logtable
values (:new.pk_workerid , sysdate );
END;
To remove the apparent confusion, SELECT ... INTO populates a local (PL/SQL) variable. It is not the syntax we use for inserting into a table.

Oracle trigger insert other table

I have 2 tables which are my_school and my_class
And "my_school" table has 'info_id' column and also "my_class" table has 'info_id' then I want to get a query that automatically generate "info_id" then I found solution..
Here are my working TRIGGER on "my_school" table...
CREATE OR REPLACE TRIGGER info_id
before insert on my_direction
for each row
begin
if :NEW.WAY_ID is null then
:NEW.WAY_ID := example_id_seq.nextval;
end if;
end;
It works and it's generating auto id when inserting value.
But now how to get this trigger do it on "my_class" table when users insert value on my_school's table then take id with "my_class" table's "info_id" column same time?
You can create trigger on my_school table to update info_id similar to that you have explained and while inserting records, use returning into clause.
Declare a variable to store returned value, for example
v_info_id number(9);
And use it in returning into clause
insert into my_school(column.......list)
values (values........list)
RETURNING info_id INTO v_info_id;
Use v_info_id in your program to insert value of info_id into another tables.