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);
Related
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;
I'm trying to create after insert trigger to delete a row from table whenever I insert the same row in the opposite table using the ID
But it doesn't work
Create Trigger [dbo].[MeritedDeceased] On [dbo].[Deceased]
After Insert
As
Begin
Delete From dbo.Merited Where dbo.Deceased.ID = dbo.Merited.ID
End
I think you mean to do something like this (maybe)...
CREATE TRIGGER [dbo].[MeritedDeceased] ON [dbo].[Deceased]
AFTER INSERT
AS
BEGIN
DELETE M
FROM [dbo].[MeritedDeceased] M
INNER JOIN Inserted I
ON I.ID = M.ID
;
END
;
Translation: Every time a row is inserted into dbo.Deceased, delete any rows with the same (inserted) ID from dbo.MeritedDeceased.
FYI, this TRIGGER won't just delete one row, but also a batch of rows that were inserted together. If that's what you want, then this should help.
I have two tables:
cost_bank (this is where I want the trigger)
master_data (this table has information I need to check)
I want to build an "after insert" trigger on cost_bank which runs when the newly inserted cost_bank.work_code is in the master_data table with a master_data.charge_type = 'EX'. If that scenario is true, I want the trigger to update the newly added row only (can specify by ID) in cost_bank with the value from master_data.r_cost_center for cost_bank.cost_center.
Here is what I have so far:
CREATE OR REPLACE TRIGGER expense_charge_cost_center
AFTER INSERT ON cost_bank FOR EACH ROW
WHEN (new.work_code in (select work_code from master_data where charge_type = 'EX'))
BEGIN
update cost_bank set cost_center = (select r_cost_center
from master_data where work_code = new.work_code) where id = new.id;
END;
Any insight is appreciated
If you want to modify the same table, you want a before insert trigger:
CREATE OR REPLACE TRIGGER expense_charge_cost_center
BEFORE INSERT ON cost_bank FOR EACH ROW
WHEN (new.work_code in (select work_code from master_data where charge_type = 'EX'))
BEGIN
select r_cost_center into :new.cost_center
from master_data
where work_code = new.work_code;
END;
Actually, I'm not sure if subqueries work with when, so you might need:
CREATE OR REPLACE TRIGGER expense_charge_cost_center
BEFORE INSERT ON cost_bank FOR EACH ROW
BEGIN
if (new.work_code in (select work_code from master_data where charge_type = 'EX')) then
select r_cost_center into :new.cost_center
from master_data
where work_code = new.work_code; -- I'm guessing you also want charge_type = 'EX'
end if;
END;
I have created two tables customer and orders and I have already inserted some data in customer table but nothing is in orders table. Now I want to create a trigger on orders table that would copy only the id column ( which is defined in customer table as primary key) in orders table of o_id ( which is defined as foreign key in orders table ).
I want to check my created trigger and fix it if you find any error in it.
My trigger for orders table :
CREATE OR REPLACE TRIGGER tri_order
BEFORE INSERT
ON orders
FOR EACH ROW
BEGIN
SELECT ID
INTO :NEW.o_id
FROM customer;
END;
Thanks.
I want to create a trigger on orders table that would copy only the
id column
you can start doing like this , (by the way I didnt understand where you want to use your derived table ? what is the query)
CREATE OR REPLACE TRIGGER tri_order
BEFORE INSERT
ON orders
FOR EACH ROW
declare cnt number (3);
BEGIN
select count(1) into cnt from customer where id=:new.id; // its is checking
if cnt=0 then
insert into customer (id) values (:new.id);
end if;
// you can add raise error here
END;
/
you want to copy a value from customer table to orders table with help of trigger so firstly you will be needing to create the trigger on customertable not on orderstable ,since for every insert done in the customer table the trigger will be fired once which will insert a value in orders table.
CREATE OR REPLACE TRIGGER tri_order
AFTER INSERT
ON customer
FOR EACH ROW
BEGIN
INSERT INTO orders
(o_id)
SELECT :NEW.ID
FROM customer;
END;
You must specify a rule for finding the customer.
For example:
CREATE OR REPLACE TRIGGER tri_order
BEFORE INSERT
ON orders
FOR EACH ROW
BEGIN
SELECT c.ID
INTO :NEW.o_id
FROM customer c
WHERE c.customer_code = :NEW.customer_code;
END;
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