I am looking to create a trigger that will bring back staff id when a row is inserted.
I have this so far:
CREATE OR REPLACE TRIGGER insert_trigger
BEFORE
INSERT ON staff
FOR EACH ROW
DECLARE
v_staff_id VARCHAR2(5);
BEGIN
SELECT staff_id from staff INTO v_staff_id FROM dual;
END;
I keep getting errors like sql statment ignored and command not ended properly for this line:
v_staff_id VARCHAR2(5);
Any help would be appreciated.
Thanks
You can access the new/altered data(inserted/update data) by using semi-column ':'
As you are writing trigger while inserting the data you need to use :new.column_name.
In the example it is :new.staff_id.
For complete reference please check
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7004.htm
Related
I'm taking my first steps in Pl/SQL and am struggling with triggers. I've tried creating the trigger below but am receiving this error:
Error at line 2: PL/SQL: SQL Statement ignored
PL/SQL: ORA-00942: table or view does not exist
To clarify: I have checked the name of the table over and over, and it does exist. It is also in the same schema as the trigger I'm trying to create. The 'customer_seq.NEXTVAL' refers to a sequence created previously that runs without errors.
The code is as follows:
CREATE TRIGGER new_customer
BEFORE INSERT ON customer
FOR EACH ROW
BEGIN
INSERT INTO customer_id VALUES ('c-', customer_seq.NEXTVAL);
END;
Thanks in advance for any help.
You probably intend something like this:
CREATE TRIGGER new_customer
BEFORE INSERT ON customer
FOR EACH ROW
BEGIN
SELECT customer_seq.NEXTVAL INTO :NEW.customer_id
FROM dual;
END;
It is unclear what the purpose of 'C_' is. If it is part of the customer id, I would advise you to stick to numbers.
Also note that more recent versions of Oracle support generated always as identity -- which is much preferred over defining a sequence and trigger.
I want to create an update trigger for a table. The trigger was created, but when I update the column finish, it say mutating table.
This is my code
CREATE OR REPLACE TRIGGER SET_COST BEFORE UPDATE OF finish ON PAY
FOR EACH ROW
BEGIN
UPDATE PAY
SET PAY.COST = (finish-start) * 20000
WHERE PAY.ID=:new.ID;
END;
This trigger gives me 'Mutating Table' error and so far I have been unable to fix it. Any Suggestion ? Thanks
First of all, you shouldn't be doing that at all. There's no use in storing values that are easily calculated whenever you need them.
As of your question: no UPDATE:
begin
:new.cost := (:new.finish - :new.start) * 20000;
end;
Trying to create a trigger that will update a clob field to rtrim hard returns the application is tossing in.
Cannot get to the application code, so I see no other way to make potentially needed changes to the data with a trigger.
The returns are not always added.
This code is throwing error ....
Inconsistent data type expected got clob.
I thought declaring the field would avoid the issue... but no...
Any help or suggestions greatly appreciated.
CREATE OR REPLACE TRIGGER AI_master_set
AFTER INSERT ON base1
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
master_set CLOB;
BEGIN
UPDATE base1
set master_set= rtrim(master_set,chr(00))
WHERE master_set = :new.master_set;
:new.master_set:= master_set;
END;
Based on the horse with no name, and on Oracle (11g) compound trigger not updating CLOB data field, this should simply work:
CREATE OR REPLACE TRIGGER AI_master_set
before INSERT ON base1
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
tmp_master_set CLOB;
BEGIN
tmp_master_set := rtrim(:new.master_set,chr(00));
:new.master_set:= tmp_master_set ;
END;
/
Hope it helps.
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;
I'm using pl\sql developer and I have a report table with a number(38) ID column.
I want to keep track of all updates for this table so I created another table like this:
CREATE TABLE reportUpdate (report_id number(38), updatedate number(32));
And I created a trigger:
CREATE or REPLACE TRIGGER BeforeUpdateReport
BEFORE
UPDATE ON REPORT
FOR EACH ROW
Begin
INSERT INTO reportUpdate
Values(old.ID,sysdate);
END;
And when I run it, I get an error, saying: trigger 'SYSTEM.BEFOREUPDATEREPORT' is invalidand failed re-validation.
Can someone please help
You can use show errors after you see compiled with warnings, or query the user_errors view to see what is wrong later.
One obvious thing is that you haven't prefixed the old reference with a colon:
CREATE or REPLACE TRIGGER BeforeUpdateReport
BEFORE
UPDATE ON REPORT
FOR EACH ROW
Begin
INSERT INTO reportUpdate
Values(:old.ID,sysdate);
END;
/
It's also better to specify the target table fields in the insert statement:
INSERT INTO reportUpdate (report_id, updatedate)
Values(:old.ID,sysdate);
But you have update_date defined in your table creation script as number(32), which doesn't make sense. As #realspirituals pointed out, it should be:
CREATE TABLE reportUpdate (report_id number, updatedate date);