How to fix SQL Trigger - sql

How to fix it?
CREATE TRIGGER tb_mhs2_hapus AFTER DELETE
ON tb_mhs2 FOR EACH ROW
BEGIN
INSERT INTO tb_mhs2_hapus (nama, no, alamat, org_tua, timeee) VALUES (old.nama, old.no, old.alamat, old.org_tua, SYSDATE());
END;
im confused where im wrong? "You have an error in your SQL syntax;"

First of all why your trigger name is same as table name? tb_mhs2_hapus
So your code should be:
CREATE OR REPLACE TRIGGER TRG_tb_mhs2_hapus
AFTER DELETE ON tb_mhs2
FOR EACH ROW
BEGIN
INSERT INTO tb_mhs2_hapus (nama, no, alamat, org_tua, timeee)
VALUES (old.nama, old.no, old.alamat, old.org_tua, SYSDATE());
END;

Related

ORACLE SQL: After Update Trigger

Problem Statement:
Create a trigger named trigger_contact_af_update that is triggered whenever the contact table is updated. This trigger will insert the org_name and action into the table contact_log_history after the update of contact details. The action name in the affected log table contact_log_history is After_Update_Contact.
The query I have at the moment is:
CREATE OR REPLACE TRIGGER trigger_contact_af_update
AFTER UPDATE ON contact
FOR EACH ROW
BEGIN
INSERT INTO contact_log_history (org_name, action) Values (:OLD.org_name, 'After_Update_Contact');
END;
But it's not working. Can someone please tell me why?
The table contact_log_history has only two columns.
Using the :OLD variable only works when using a before trigger. I would recommend you to do that in your case.
CREATE OR REPLACE TRIGGER trigger_contact_af_update
BEFORE UPDATE ON contact
FOR EACH ROW
BEGIN
INSERT INTO contact_log_history (org_name, action) Values (:old.org_name, 'After_Update_Contact');
END;

ORA-04071: missing BEFORE, AFTER or INSTEAD OF keyword

I am writing some PL/SQL code for a apex database application. With the code a want to realise that when I make a purchase order, a purchase orderline is automatically generated based on the purchase order_id. However, I'm getting a ORA-04071 error running the code below:
create or replace trigger "INKOOPORDER_T1"/
AFTER insert or update or delete on INKOOPORDER
for each row begin
INSERT INTO INKOOPORDERREGEL
(I_nummer)
SELECT
I_nummer
FROM inkooporderregel
go
end/
Can somebody help me please?
ur query has some syntax errors
try below code
I removed / from first line and after End, put ; at the end of insert statement and deleted go.Also after end a ;
create or replace trigger "INKOOPORDER_T1"
AFTER insert or update or delete on INKOOPORDER
for each row
begin
INSERT INTO INKOOPORDERREGEL
(I_nummer)
SELECT
I_nummer
FROM inkooporderregel;
end;

Trigger has mutating error when insert

I am having an issue with a trigger. After I add the trigger to my database, when I try to insert a row that the trigger will act on, I get a mutating table error. I don't know how to avoid this. Would someone mind looking at this and tell me what I am doing wrong, and how to rewrite it?
CREATE OR REPLACE TRIGGER ORNG_INV_LINE_TOTAL_TRIGGER
FOR INSERT OR UPDATE ON ORNG_INV_LINE
COMPOUND TRIGGER
AFTER EACH ROW IS
BEGIN
UPDATE ORNG_INVOICE SET Inv_Amount = (SELECT SUM(ORNG_INV_LINE.Inv_Line_Total)
FROM ORNG_INV_LINE
WHERE ORNG_INVOICE.INV_Num = :NEW.INV_Num);
END AFTER EACH ROW;
END ORNG_INV_LINE_TOTAL_TRIGGER;
/
I'm not sure why it is triggering an error. I'm trying to do the action after the update. All I want to do is get the sum for all of the lines of all matching invoice numbers, and write that value in the INVOICE table. Thanks for the help.
Your trigger is written on the table ORNG_INV_LINE for insert or update and again you made a select on the same table while updating ORNG_INVOICE table so trigger is mutating,
to over come this you have to use statement level trigger instead of row level trigger .
i.e after each statement should be there in code instead of after each row.
This can help you.
Here is the solution...apparently everyone is busy with Thanksgiving. Hopefully this helps the next guy.
create or replace TRIGGER ORNG_INV_L_TTL_TRIG
AFTER INSERT OR UPDATE OR DELETE ON ORNG_INV_LINE
BEGIN
UPDATE ORNG_INVOICE SET Inv_Amount = (SELECT SUM(Inv_Line_Total)
FROM ORNG_INV_LINE
WHERE INV_Num = ORNG_INVOICE.INV_Num);
END ORNG_INV_L_TTL_TRIG;
/

Oracle SQL trigger for automatically set a column value

I am writing a Oracle trigger. This trigger should automatically set the value of the column "productId" to be the oid of the row just inserted.
The trigger I wrote is:
create or replace trigger MyProduct_id_trg
after insert on MyProduct
begin
update MyProduct set productId = inserted.oid where oid = inserted.oid;
end;
However, this does not work.
Can someone help me with this?
Regards.
Looks like you are trying to use SQL Server syntax on an Oracle database! Try this:
create or replace trigger MyProduct_id_trg
before insert on MyProduct
for each row
begin
:new.productId := :new.oid;
end;
(Note: before not after, and with for each row.)

Oracle Trigger on Insert or Delete or Update

Trying to create an Oracle trigger that runs after a table is updated in any way. I've been googling this all morning and came up with this:
CREATE OR REPLACE TRIGGER gb_qty_change
AFTER UPDATE OR INSERT OR DELETE ON F_ITEM_STORE
FOR EACH ROW
DECLARE
v_qty V_AD_ON_HAND%rowtype;
v_isbn TD_ITEM_DESCRIPTION.TD_IDENTIFIER%type;
BEGIN
delete from gb_transaction where gb_tide = :new.ITST_ITEM_TIDE_CODE;
select TD_IDENTIFIER INTO v_isbn from TD_ITEM_DESCRIPTION where TD_TIDE = :new.ITST_ITEM_TIDE_CODE;
select * INTO v_qty from V_AD_ON_HAND where ITST_ITEM_TIDE_CODE = :new.ITST_ITEM_TIDE_CODE;
insert into gb_transaction(gb_tide, gb_isbn, gb_used_on_hand, gb_new_on_hand)
values(:new.ITST_ITEM_TIDE_CODE, v_isbn, v_qty.USED_ON_HAND, v_qty.NEW_ON_HAND);
END;
/
I'm trying to keep it to a single record per TIDE_CODE in the new table.
V_AD_ON_HAND is a view that pulls an inventory count.
gb_transaction is my new table where I'm logging these events
Comparing it to other peoples code it looks like it should run but I'm getting "Warning: Trigger created with compilation errors."
The problem, I believe is with the :new designations for a delete trigger. There is, after all, no NEW value as the record is expunged. You can only access the :OLD values on delete.
if you section the trigger by operation, you can do this.
CREATE OR REPLACE TRIGGER gb_qty_change
AFTER UPDATE OR INSERT OR DELETE ON F_ITEM_STORE
FOR EACH ROW
DECLARE
v_qty V_AD_ON_HAND%rowtype;
v_isbn TD_ITEM_DESCRIPTION.TD_IDENTIFIER%type;
BEGIN
IF INSERTING or UPDATING then
... insert your existing code
ELSE
... do something similar with the :old values for the deleting case
end if;
END;
/
Incidentally, it is generally helpfull if you tell us WHAT the error is, not just that you had one. If compiling via SQL*Plus script, after the forward slash call to compile the trigger after the "end;" statement, add a line that says:
SHOW ERRORS TRIGGER YOUR_TRIGGER_NAME;