I'm trying to figure out a simple ORACLE PL/SQL programming problem and I'm having some difficulties with it.
I have to make a trigger that catches inserts into a table, and if the location attribute of the new tuple getting into that table doesn't exist in the database, I need to throw a warning message and insert that new location into another table.
What I have now so far -
CREATE TRIGGER sightTrigger
AFTER INSERT ON SIGHTINGS
FOR EACH ROW
DECLARE
ct INTEGER;
BEGIN
SELECT COUNT(*)
INTO ct
FROM SIGHTINGS
WHERE SIGHTINGS.location <> :NEW.location;
IF ct > 0 THEN
RAISE_APPLICATION_ERROR('WARNING SIGN' || :NEW.location ||' does not exist in the database');
INSERT INTO FEATURES(LOCATION, CLASS, LATITUDE, ...)
VALUES (:NEW.LOCATION, 'UNKNOWN', ...);
END IF;
END;
I'm getting an error, "PLS-00306: wrong number of types of arguments in call to 'RAISE_APP_ERROR'. Could somebody tell me what's wrong? thank you
Try this:
RAISE_APPLICATION_ERROR(
-20001,
'WARNING SIGN' || :NEW.location || 'does not exist in the database'
);
Your RAISE_APPLICATION_ERROR takes two arguments (this is from Oracle docs): where error_number is a negative integer in the range -20000 .. -20999 and message is a character string up to 2048 bytes long.
Related
Hello fellow programmers and happy new year to you all!
I have few university tasks for winter break and one of them is to create trigger on table:
PERSON(ID, Name, Surname, Age);
Trigger is supposed to inform user when they have inserted row with invalid ID. Vadility criteria is that ID is 11 digits long.
I tried to write solution like this:
CREATE OR REPLACE TRIGGER person_id_trigg
AFTER INSERT
ON person
DECLARE
idNew VARCHAR(50);
lengthException EXCEPTION;
BEGIN
SELECT id INTO idNew FROM INSERTED;
IF LENGTH(idNew) <> 11 THEN
RAISE lengthException;
END IF;
EXCEPTION
WHEN lengthException THEN
dbms_output.put_line('ID for new person is INVALID. It must be 11 digits long!');
END;
Then I realized that INSERTED exists only in sqlserver and not in oracle.
What would you suggest I could do to fix that?
Thanks in advance!
Do you want to raise an exception (which would prevent the insert from succeeding)? Or do you want to allow the insert to succeed and write a string to the dbms_output buffer that may or may not exist and may or may not be shown to a human running the insert?
In either case, you'll want this to be a row-level trigger, not a statement-level trigger, so you'll need to add the for each row clause.
CREATE OR REPLACE TRIGGER person_id_trigg
AFTER INSERT
ON person
FOR EACH ROW
If you want to raise an exception
BEGIN
IF( length( :new.id ) <> 11 )
THEN
RAISE_APPLICATION_ERROR( -20001,
'The new ID value must have a length of 11' );
END IF;
END;
If you want to potentially print output but allow the insert to succeed
BEGIN
IF( length( :new.id ) <> 11 )
THEN
dbms_output.put_line( 'The new ID value must have a length of 11' );
END IF;
END;
Of course, in reality, you would never use a trigger for this sort of thing. In the real world, you would use a constraint.
I have 2 tables, Contract and Bankslip.
I need to get the date field from the Contract table, and set the date on Bankslip table, but it's getting in a loop, I think!
How can i do it?
Here is my code:
create or replace TRIGGER GFLANCAM_ATUALIZA_DATA_EMISSAO
BEFORE INSERT ON GFLANCAM
FOR EACH ROW
DECLARE
DATA_INICIO_CONTRATO DATE;
BEGIN
CASE WHEN :NEW.DOCUMENTO <> ' ' then
SELECT dt_inicio
INTO DATA_INICIO_CONTRATO
from ctcontra
where cd_contrato = :NEW.documento;
:NEW.data := DATA_INICIO_CONTRATO;
END CASE;
END;
What am I doing wrong?
Much of the trigger is unnecessary.
You can accomplish your goal without the CASE and without defining a variable.
CREATE OR REPLACE TRIGGER GFLANCAM_ATUALIZA_DATA_EMISSAO
BEFORE INSERT
ON GFLANCAM
FOR EACH ROW
BEGIN
-- Consider following:
-- IF NVL (:NEW.DOCUMENTO, ' ') <> ' '
IF :NEW.DOCUMENTO <> ' '
THEN
-- Following line may cause ORA-01403: no data found
SELECT dt_inicio INTO :NEW.data FROM ctcontra WHERE cd_contrato = :NEW.documento;
END IF;
END;
/
A few notes:
If you want to catch NULL values then add the NVL shown above.
Watch out for the case where a corresponding record is not found in ctcontra--this condition would result in ORA-01403: no data found (which might be exactly what you want in this case).
Make sure that ctcontra has only one record for each cd_contrato value, otherwise you will get a ORA-01422: exact fetch returns more than requested number of rows.
Take a look at the update:
{CREATE OR REPLACE TRIGGER GFLANCAM_ATUALIZA_DATA_EMISSAO
AFTER INSERT ON GFLANCAM
FOR EACH ROW
DECLARE
DATA_INICIO_CONTRATO DATE;
BEGIN
IF DOCUMENTO <> ' ' THEN
SELECT dt_inicio INTO DATA_INICIO_CONTRATO from ctcontra where cd_contrato =
DOCUMENTO;
UPDATE GFLANCAM SET DATA = DATA_INICIO_CONTRATO;
END IF;
END;}
I am trying to implement a statement level trigger to enforce the following "An applicant cannot apply for more than two positions in one day".
I am able to enforce it using a row level trigger (as shown below) but I have no clue how to do so using a statement level trigger when I can't use :NEW or :OLD.
I know there are alternatives to using a trigger but I am revising for my exam that would have a similar question so I would appreciate any help.
CREATE TABLE APPLIES(
anumber NUMBER(6) NOT NULL, /* applicant number */
pnumber NUMBER(8) NOT NULL, /* position number */
appDate DATE NOT NULL, /* application date*/
CONSTRAINT APPLIES_pkey PRIMARY KEY(anumber, pnumber)
);
CREATE OR REPLACE TRIGGER app_trigger
BEFORE INSERT ON APPLIES
FOR EACH ROW
DECLARE
counter NUMBER;
BEGIN
SELECT COUNT(*) INTO counter
FROM APPLIES
WHERE anumber = :NEW.anumber
AND to_char(appDate, 'DD-MON-YYYY') = to_char(:NEW.appDate, 'DD-MON-YYYY');
IF counter = 2 THEN
RAISE_APPLICATION_ERROR(-20001, 'error msg');
END IF;
END;
You're correct that you don't have :OLD and :NEW values - so you need to check the entire table to see if the condition (let's not call it a "constraint", as that term has specific meaning in the sense of a relational database) has been violated:
CREATE OR REPLACE TRIGGER APPLIES_AIU
AFTER INSERT OR UPDATE ON APPLIES
BEGIN
FOR aRow IN (SELECT ANUMBER,
TRUNC(APPDATE) AS APPDATE,
COUNT(*) AS APPLICATION_COUNT
FROM APPLIES
GROUP BY ANUMBER, TRUNC(APPDATE)
HAVING COUNT(*) > 2)
LOOP
-- If we get to here it means we have at least one user who has applied
-- for more than two jobs in a single day.
RAISE_APPLICATION_ERROR(-20002, 'Applicant ' || aRow.ANUMBER ||
' applied for ' || aRow.APPLICATION_COUNT ||
' jobs on ' ||
TO_CHAR(aRow.APPDATE, 'DD-MON-YYYY'));
END LOOP;
END APPLIES_AIU;
It's a good idea to add an index to support this query so it will run efficiently:
CREATE INDEX APPLIES_BIU_INDEX
ON APPLIES(ANUMBER, TRUNC(APPDATE));
dbfiddle here
Best of luck.
Your rule involves more than one row at the same time. So you cannot use a FOR ROW LEVEL trigger: querying on APPLIES as you propose would hurl ORA-04091: table is mutating exception.
So, AFTER statement it is.
CREATE OR REPLACE TRIGGER app_trigger
AFTER INSERT OR UPDATE ON APPLIES
DECLARE
cursor c_cnt is
SELECT 1 INTO counter
FROM APPLIES
group by anumber, trunc(appDate) having count(*) > 2;
dummy number;
BEGIN
open c_cnt;
fetch c_cnt in dummy;
if c_cnt%found then
close c_cnt;
RAISE_APPLICATION_ERROR(-20001, 'error msg');
end if;
close c_cnt;
END;
Obviously, querying the whole table will be inefficient at scale. (One of the reasons why triggers are not recommended for this sort of thing). So this is a situation in which we might want to use a compound trigger (assuming we're on 11g or later).
I'm stuck and can't see what the problem is.
I created a procedure with some logic stuff and it compiled successfully, but when I call it in a trigger on my table, it fails with ORA-01722: invalid number.
Any help would be gratefully received.
Here is the procedure:
create or replace Procedure Check_Plants_Fields(
field_id IN number ,
farmer_name IN varchar2,
planting_date IN date DEFAULT sysdate,
planting_amount IN number,
plant_type IN number)
IS
l_plant_type XXLA_PLANTS_TYPE.PLANT_TYPE%type;
l_number_of_months XXLA_PLANTS_SUPPLIERS.NUMBER_MONTHS_TO_CUT%type;
cursor c1 is
SELECT PLANT_TYPE , AMOUNT_
FROM XXLA_PLANTS_TYPE
WHERE PLANT_ID = plant_type;
cursor c2 is
SELECT to_number(MOD(Round(DBMS_RANDOM.Value(1, 8)), 9) + 1)
FROM DUAL;
BEGIN
open c1;
open c2;
fetch c2 into l_number_of_months;
if c1 %notfound then
close c1;
dbms_output.put_line('You dont have this type, please supply it to your store . ' || ' ' || 'thanx.');
end if;
for i in (SELECT PLANT_TYPE , AMOUNT_
FROM XXLA_PLANTS_TYPE
WHERE PLANT_ID = plant_type)
LOOP
if i.AMOUNT_ < 20 then
dbms_output.put_line(i.AMOUNT_ ||' AMOUNT_: '|| i.AMOUNT_);
UPDATE XXLA_PLANTS_SUPPLIERS
SET supplier_id = supplier_id_seq.nextval;
INSERT INTO XXLA_PLANTS_SUPPLIERS
(supplier_id, supplier_name, number_months_to_cut, date_,amount, price_in_KG)
VALUES (supplier_id_seq.nextval, 'Benefits etc',l_number_of_months ,sysdate+1, 80 + i.AMOUNT_,'500$');
end if;
END LOOP i;
commit;
close c2;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered,- '||SQLCODE||' -ERROR- -->'||SQLERRM );
END;
And here is the trigger:
create or replace TRIGGER xxbefor_insert_plants
BEFORE INSERT ON XXLA_PLANT_FIELDS
FOR EACH ROW
BEGIN
Check_Plants_Fields(:NEW.field_id, :NEW.farmer_name, :NEW.planting_date, :NEW.planting_amount, :NEW.plant_type);
END;
Here is the insert that executes the trigger that calls the procedure, but it errors when I run it.
INSERT INTO XXLA_PLANT_FIELDS
(field_id, FARMER_NAME,planting_date,planting_amount,plant_type )
VALUES
(4, 'Test1',sysdate,8,1 (because this parameter it should work its under 20 ));
ORA-01722: invalid number is quite a simple error to understand. It means you are attempting to cast a string to a number datatype but the operation fails because the string contains non-numeric characters.
So what this means is that somewhere in your procedure you have a implicit type conversion. You haven't provided the description of the tables so we can't tell you where the problem happens. You'll have to discover it for yourself.
Two places where it might be:
WHERE PLANT_ID = plant_type if XXLA_PLANTS_TYPE.PLANT_ID is not
numeric
VALUES (... '500$'); if
XXLA_PLANTS_SUPPLIERS.PRICE_IN_KG is not numeric
Incidentally, if you removed that pointless exception handler you would get the default error stack. That would tell you the starting line number of the failing statement, which would really remove a lot of the guesswork from the exercise.
I'm on Oracle DB 12c and using SQL Developer.
How can I display every rows of a table inside a procedure, if all arguments are "null".
I'm guessing that my IF is correct and I might have seen two or three posts where they used cursors but I'm not very familiar with the utilization of those.
I would like it to basically be a SELECT * FROM Salle but with a condition.
Am I on the right track here ?
CREATE OR REPLACE PROCEDURE Foo1 (noSalle in varchar2, Cat in varchar2, Nb in number)
IS
cursor SYS_REFCURSOR; -- Not sure at all about that
BEGIN
DBMS_OUTPUT.PUT_LINE('Salle : ' || noSalle || 'Cat : ' || Cat);
IF (noSalle = null AND Cat = null AND Nb = null) THEN
OPEN cursor FOR
SELECT * from Salle;
-- Some sort of FOR row IN cursor LOOP ?
-- Display ALL rows of "Salle"
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Erreur Oracle : '||sqlcode||' ; Message Oracle : '||sqlerrm);
END;
SYS_REFCURSOR is used to hold tabular data usually used when you want to return such data from a function.
Null comparison is incorrect. Use colname is null instead.
If you just want to print all the rows using dbms_output.put_line or something similar, you can do this
for row in (select * from salle) loop
dbms_output.put_line(row.col1||' '||row.col2);
end loop;