Why doesn't this trigger work properly? - sql

From what I have read about triggers and how they work, I thought that this trigger would insert data into the relations related to my table phonenumber after I insert values into it. I'm using dbms_random to create a random 5 digit usageID not already in the usage table (or atleast that's what I had thought it would do).
create or replace TRIGGER addPhoneLine
AFTER INSERT ON phoneNumber
REFERENCING NEW AS NEW
FOR EACH ROW
DECLARE
primNum varchar(12);
acctNum numeric(5);
NEWusageID_new varchar(5);
BEGIN
if :new.primaryNumber is not NULL then
select acctID, primaryNumber into acctNum, primNum
from account A
where A.primaryNumber = :new.primaryNumber;
select to_char(round(dbms_random.value(10000, 99999),0)) into
NEWusageID_new from dual
minus
select usageID from usage;
INSERT INTO acct_num VALUES
(acctNum, primNum, :new.phonenumber);
INSERT INTO phone_usage VALUES
(NEWusageID_new, :new.phonenumber);
end if;
END;
But it throws the following errors when I attempt to insert into the phoneNumber table:
ORA-01403: no data found
ORA-06512: at "ADDPHONELINE", line 9
ORA-04088: error during execution of trigger 'ADDPHONELINE'
The relevant tables were created as follows:
create table phoneNumber(phoneNumber varchar(12) PRIMARY KEY, primaryNumber varchar(12));
create table acct_num(acctID numeric(5) references ACCOUNT, primaryNumber varchar(12) references ACCOUNT, phoneNumber varchar(12) references phoneNumber);
create table phone_usage(usageID varchar(5) references USAGE, phoneNumber varchar(12) references PHONENUMBER)

Your trigger is based on an insert on table phoneNumber and the error "No Data Found" is thrown when a SELECT INTO is used and it doesnt find any information to insert.
So the problem must be this statement.
select acctID, primaryNumber into acctNum, primNum
from account A
where A.primaryNumber = :new.primaryNumber;
Are you certain that the :new.primaryNumber exists in the account table when this trigger is active?
Maybe you are only populating the account table after this insert is complete?

The below query is returning no rows, as per error description you posted. Either your :new.primaryNumber is incorrect or there is no matching record in the account table.
Begin
select acctID, primaryNumber into acctNum, primNum
from account A
where A.primaryNumber = :new.primaryNumber;
exception when others then
insert your favorite logging method here logging out your :new.primaryNumber
end;

I show you here what will happen when dbms_random gives a value (NEWusageID_new), which exists in the usage:
DECLARE
i NUMBER;
BEGIN
SELECT 15 INTO i FROM DUAL
MINUS
SELECT 15 FROM DUAL;
END;
ORA-01403: no data found
ORA-06512: in line 4
Use a sequence instead.

Related

Auto-increment in Oracle table when insert in the same table

I have a table and I want to increment a column by 1 when I insert a row into the same table.
Table users - when I insert first row value of idusers is 1, and in second row value is 2 ....
This is the table
USERS
EMAIL primary key
USERNAME
PASSWORD
IDUSER and this the column I want to be AUTO_INCREMENT
I have tried this code
CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10;
create or replace trigger incrementIdUser
before insert on users
for each row
begin
select seq_person.nextval into :new.IDUSER from users;
end;
But I get an error when I insert a row:
Erreur lors de l'enregistrement des modifications de la table "SOCIAL"."USERS" :
Ligne 1 : ORA-01403: no data found
ORA-01403: no data found
ORA-06512: at "SOCIAL.INCREMENTIDUSER", line 2
ORA-04088: error during execution of trigger 'SOCIAL.INCREMENTIDUSER'
ORA-06512: at line 1
Not like that, but
create or replace trigger incrementIdUser
before insert on users
for each row
begin
:new.iduser := seq_person.nextval;
end;
Code you wrote selects from users table (which is empty, hence NO_DATA_FOUND). If it contained more than a single row, you'd get TOO_MANY_ROWS (as you're selecting into a scalar variable (:new.iduser). Finally, there's danger of mutating table error as you can't select from a table which is just being modified (in this trigger type).
Insetead of select seq_person.nextval into :new.IDUSER from users; to assign sequence value into iduser you need to use :new.IDUSER :=seq_person.nextval;
create or replace trigger incrementIdUser
before insert on users
for each row
begin
:new.IDUSER :=seq_person.nextval;
end;
You get that error because there are zero rows in the USERS table so SELECT ... FROM USERS returns no rows.
What you want is to either use a table that will always return a single row (which, in Oracle, is the DUAL table):
create or replace trigger incrementIdUser
before insert on users
for each row
begin
select seq_person.nextval into :new.IDUSER from DUAL;
end;
Or, the better solution, is to not use an SQL statement and use pure PL/SQL:
create or replace trigger incrementIdUser
before insert on users
for each row
begin
:new.IDUSER := seq_person.nextval;
end;
Instead of using a trigger, you should use an identity column in the create table statement:
create table users
(iduser integer generated by default on null as identity (nomaxvalue nocache order),
...);

SQL - Trigger is invalid and failed re-validation

When a user creates an application for the table 'application' I want a copy placed into the table 'application_history'. I've tried creating a trigger and get the error that is in the title.
create or replace TRIGGER create_history AFTER INSERT ON application
FOR EACH ROW
DECLARE
BEGIN
insert into application_history values (:new.arn, :new.srn, :new.job_id, :new.application_status);
END;
Any help will be much appreciated.
As Justin suspected, you seem to be having more than 4 columns in application_history. Either provide values for all columns or specify columns lie 'insert into table1 (col1, col2) values (1,2)'
It worked for me after adding extra column 'hist_id'(just to give you an idea) ...
create table application (arn number, srn number, job_id number, application_status char(1) );
create table application_history (hist_id number, arn number, srn number, job_id number, application_status char(1) );
create or replace TRIGGER create_application_history AFTER INSERT ON application
FOR EACH ROW
DECLARE
BEGIN
insert into application_history values (1, :new.arn, :new.srn, :new.job_id, :new.application_status);
END;
/
I think you need a / at the end. So:
create or replace TRIGGER create_history AFTER INSERT ON application
FOR EACH ROW
DECLARE
BEGIN
insert into application_history values (:new.arn, :new.srn, :new.job_id, :new.application_status);
END;
/

Expected NUMBER got DATE

I am getting an SQL error that makes no sense to me. I created a table called SUBORDER using this command:
CREATE TABLE SUBORDER (
OrderNo int,
SuborderNo int,
ReqShipDate date,
ActualShipDate date,
BranchName varchar(50),
primary key (OrderNo, SuborderNo),
foreign key (OrderNo) references ORDERS(OrderNo) on delete set null,
foreign key (BranchName) references BRANCH(BranchName) on delete set null
);
As you can see, ReqShipDate and ActualShipDate were declared as dates. But then when I run this code:
create or replace trigger shipment_late
before update
on SUBORDER
for each row
declare
reqdate DATE;
actualdate DATE;
begin
select s.ReqShipDate, s.ActualShipDate into reqdate, actualdate
from SUBORDER s
where ( s.OrderNo = :new.OrderNo ) and ( s.SuborderNo = :new.SuborderNo );
if reqdate > actualdate then
insert into LATE_SHIPMENT
select * from SUBORDER s
where ( s.OrderNo = :new.OrderNo ) and ( s.SuborderNo = :new.SuborderNo );
end if;
end;
the SQL console tells me:
Error(12,11): PL/SQL: ORA-00932: inconsistent datatypes: expected
NUMBER got DATE
Why is it expecting a number?
It appears that you just want
create or replace trigger shipment_late
before update
on SUBORDER
for each row
begin
if :new.ReqShipDate > :new.ActualShipDate
then
insert into LATE_SHIPMENT( <<list of columns>> )
values( :new.OrderNo, :new.SuborderNo, ... );
end if;
end;
A couple things to be aware of
It's always a good idea to explicitly list out the columns if you're doing an insert statement. Otherwise, you're relying on an implicit order that isn't going to be obvious to someone reading your code and that creates quite a bit of potential for errors. Listing out the columns, at a minimum, documents what values you are putting where. In this example, it seems unlikely that the late_shipment table has exactly the same set of columns in suborder defined in exactly the same order.
In general, a row-level trigger on a table (SubOrder in this case) cannot query that table. Doing so will generally raise a mutating table exception. That doesn't appear to be necessary here, however. You just need to reference the various attributes of the :new pseudo-record.

Oracle SQL ORA-01403: no data found error

Hi there I already seen other posts with the same error code, but I can't figure it out.
I have this table 'NOLEGGIO' created in this way:
CREATE TABLE NOLEGGIO(
idNoleggio INT PRIMARY KEY,
dataNoleggio DATE,
dataRestituzione DATE,
dataRestituito DATE,
CF CHAR(16) NOT NULL,
prezzo NUMBER(4),
--SEVERAL CONSTRAINTS...
All I want to do now is a trigger that sets a 'dataRestituzione' := :NEW.dataNoleggio + INTERVAL '3' DAY; (that means returnDate := :NEW.rentalDATE ) IF the date of membership is < than a specific date.
I show you my 'TESSERATO' table (tesserato stands for membership)
CREATE TABLE TESSERATO(
numTessera INT NOT NULL UNIQUE,
dataTesseramento DATE,
dataScadenza DATE,
CF CHAR(16) PRIMARY KEY,
-- CONSTRAINT...
If I execute the query outside my trigger (coming next) it works (because I have datas in the fields i'm looking at) but if I insert this query in the trigger, it doesn't work!
This is the trigger:
CREATE OR REPLACE TRIGGER TR_NOLEGGIO
BEFORE INSERT ON NOLEGGIO
FOR EACH ROW
DECLARE
DATAT DATE;
BEGIN
:NEW.idNoleggio := id_noleggio.NEXTVAL;
SELECT T.dataTesseramento INTO DATAT
FROM NOLEGGIO N JOIN TESSERATO T ON N.CF=T.CF
WHERE DATAT < TO_DATE('27/02/2014','DD/MM/YYYY');
/* Here I've even tried to do something like:
IF DATAT < TO_DATE.... THEN . But it doesn't work either.
However the query that actually works if I execute outside the trigger is the SELECT above.
*/
:NEW.dataRestituzione := :NEW.dataNoleggio + INTERVAL '3' DAY;
END;
/
It says No data Found error, while there are datas in the rows instead!! (In fact doing the select outside the trigger matches several rows).
It's definitely driving me crazy ! Cannot understand what I do wrong.
Thank you in advance for anyone that get involved into this.
Insert staments for the two tables
-- NOLEGGIO
INSERT INTO NOLEGGIO VALUES(001,'18-OTT-2013','20-OTT-2013',NULL,'P3SDTI85A15H501H',10);
INSERT INTO NOLEGGIO VALUES(002,'15-NOV-2013','19-NOV-2013',NULL,'CNTNDR89T42F839M',700);
--idRental,dateRental,dateReturn,dateReturned,SSN,price)
-- TESSERATO
INSERT INTO TESSERATO(dataTesseramento,dataScadenza,CF) VALUES('07-set-2013','07-set-2014','RDLVRT70M08F205K');
-- SEVERAL INSERTS MORE
-- N.B. the numTessera is made with a sequence in another trigger
New Answer Following Comments
I have put together a test script for this. The new code used for the trigger seems to work correctly updating the return date if a valid membership exists within the date requirements set. Feel free to just take the trigger code and discard the rest, I have just included this as it is what I have used to verify that the trigger performs an update when it should:
CAUTION: I am dropping tables in this test to make it rerunable, so i would only recommend using the full script in a test environment
/**************** R U N O N C E ********************/
--CREATE OR REPLACE SEQUENCE id_noleggio
-- MINVALUE 0
-- MAXVALUE 1000000000
-- START WITH 1
-- INCREMENT BY 1
-- CACHE 20;
/********************************************************/
/****************** R E R U N A B L E ****************/
drop table NOLEGGIO;
drop table TESSERATO;
CREATE TABLE NOLEGGIO(
idNoleggio INT PRIMARY KEY,
dataNoleggio DATE,
dataRestituzione DATE,
dataRestituito DATE,
CF CHAR(16) NOT NULL,
prezzo NUMBER(4));
CREATE TABLE TESSERATO(
numTessera INT NOT NULL UNIQUE,
dataTesseramento DATE,
dataScadenza DATE,
CF CHAR(16) PRIMARY KEY);
-- TESSERATO
INSERT INTO TESSERATO(numTessera, dataTesseramento, dataScadenza, CF) VALUES(1, '15-NOV-2013','15-NOV-2014','ABCDEFGHI0000001');
INSERT INTO TESSERATO(numTessera, dataTesseramento, dataScadenza, CF) VALUES(2, '01-MAR-2014','01-MAR-2015','ABCDEFGHI0000002');
-- SEVERAL INSERTS MORE
-- N.B. the numTessera is made with a sequence in another trigger
CREATE OR REPLACE TRIGGER TR_NOLEGGIO
BEFORE INSERT ON NOLEGGIO
FOR EACH ROW
DECLARE
CUT_OFF_DATE DATE := TO_DATE('27/02/2014','DD/MM/YYYY');
MEMBER_EXISTS VARCHAR2(1) := 'N';
DATAT DATE;
BEGIN
:NEW.idNoleggio := id_noleggio.NEXTVAL;
-- membership exists
SELECT 'Y', T.dataTesseramento
INTO MEMBER_EXISTS, DATAT
FROM TESSERATO T
WHERE T.CF = :NEW.CF
AND T.dataTesseramento < CUT_OFF_DATE;
-- if value returned from query above is not null...
if MEMBER_EXISTS = 'Y' then
:NEW.dataRestituzione := :NEW.dataNoleggio + INTERVAL '3' DAY;
end if;
exception
when no_data_found then
-- e.g. if there are no records in the TESSERATO table with the same CF value
null; -- no action required, this will just stop an error being flagged
END;
/
-- test trigger
-- should set dataRestituzione (a valid membership exists within date requirements)
INSERT INTO NOLEGGIO VALUES(004, '01-Mar-2014', NULL, NULL, 'ABCDEFGHI0000001', 20); -- should set dataRestituzione
-- should not set dataRestituzione (membership too recent)
INSERT INTO NOLEGGIO VALUES(004, '01-Mar-2014', NULL, NULL, 'ABCDEFGHI0000002', 30);
-- should not set dataRestituzione (no record of membership in TESSERATO table)
INSERT INTO NOLEGGIO VALUES(1, '18-OCT-2013', NULL, NULL, 'P3SDTI85A15H501H', 10);
INSERT INTO NOLEGGIO VALUES(2, '15-NOV-2013', NULL, NULL, 'CNTNDR89T42F839M', 700);
--idRental,dateRental,dateReturn,dateReturned,SSN,price)
-- look at results
select * from TESSERATO;
select * from NOLEGGIO;
I think that the key problem with the way that you were trying to do this before is that you were joining to the NOLEGGIO table to retrieve data that had not yet been inserted.
Previous Answer
Try chaining the line:
WHERE DATAT < TO_DATE('27/02/2014','DD/MM/YYYY');
to:
WHERE T.dataTesseramento < TO_DATE('27/02/2014','DD/MM/YYYY');
It looks like you are using this variable for a where condition before you have assigned a value to it i.e. it doesn't know the value if DATAT until the query has completed, but you are trying to use this value within the query.

What is the syntax to use a Select statement inside a PL/SQL Trigger?

This is what I currently have:
CREATE OR REPLACE TRIGGER MYTRIGGER
AFTER INSERT ON SOMETABLE
FOR EACH ROW
DECLARE
v_emplid varchar2(10);
BEGIN
SELECT
personnum into v_emplid
FROM PERSON
WHERE PERSONID = :new.EMPLOYEEID;
dbms_output.put(v_emplid);
/* INSERT INTO SOMEOTHERTABLE USING v_emplid and some of the other values from the trigger table*/
END MYTRIGGER;
DBA_ERRORS has this error:
PL/SQL: ORA-00923: FROM keyword not found where expected
1) There must be something else to your example because that sure seems to work for me
SQL> create table someTable( employeeid number );
Table created.
SQL> create table person( personid number, personnum varchar2(10) );
Table created.
SQL> ed
Wrote file afiedt.buf
1 CREATE OR REPLACE TRIGGER MYTRIGGER
2 AFTER INSERT ON SOMETABLE
3 FOR EACH ROW
4 DECLARE
5 v_emplid varchar2(10);
6 BEGIN
7 SELECT personnum
8 into v_emplid
9 FROM PERSON
10 WHERE PERSONID = :new.EMPLOYEEID;
11 dbms_output.put(v_emplid);
12 /* INSERT INTO SOMEOTHERTABLE USING v_emplid and some of the other values
from the trigger table*/
13* END MYTRIGGER;
14 /
Trigger created.
SQL> insert into person values( 1, '123' );
1 row created.
SQL> insert into sometable values( 1 );
1 row created.
2) You probably want to declare V_EMPLID as being of type Person.PersonNum%TYPE so that you can be certain that the data type is correct and so that if the data type of the table changes you won't need to change your code.
3) I assume that you know that your trigger cannot query or update the table on which the trigger is defined (so no queries or inserts into someTable).
You are playing with Lava (not just fire) in your trigger. DBMS_OUTPUT in a trigger is really, really bad. You can blow-out on a buffer overflow in your trigger and the whole transaction is shot. Good luck tracking that down. If you must do output-to-console like behavior, invoke an AUTONOMOUS TRANSACTION procedure that writes to a table.
Triggers are pretty evil. I used to like them, but they are too hard to remember about. They affect data often times leading to MUTATING data (scary and not just because Halloween is close).
We use triggers to change the value of columns like .new:LAST_MODIFIED := sysdate and .new:LAST_MODIFIED_BY := user. That's it.
Don't ever allow a TRIGGER to prevent a transaction from completing. Find another option.
I would not use a select statment in a trigger ever. Insert into the table rather than a select into. Once the table already exists select into does not work in most databases.