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? - sql

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;

Related

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 get last inserted row in sap hana sql trigger?

I am creating this sql trigger in SAP HANA and cannot figure out how to reference the last inserted row, here is the code for the trigger:
CREATE TRIGGER MY_TRIGGER
AFTER INSERT ON TARGET_TABLE
FOR EACH ROW
BEGIN
CALL SOME_PROCEDURE(:NEWROW.id);
END;
I tried the REFERENCING NEW ROW syntax but it seems that it is not supported for INSERT AFTER triggers.
Help!
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;

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.

Limit number of Rows inserted into a Table

How can we restrict a table to have fixed number of rows?
for example if i give Limit as 20, then 20 rows can be inserted into it and after that table should indicate the limit has been exceeded.
EXAMPLE: IF we create a trigger
CREATE TRIGGER log AFTER INSERT ON TEST_TABLE
BEGIN
INSERT INTO TEST_TABLE VALUES(....);
SELECT COUNT(COL) FROM TEST_TABLE;
END;
This is not giving me the count as soon as i enter a row into the table.
Create a Trigger before Insert on That table.
In Trigger you can check for count for records that same table has.
Check this link for Creating Trigger.