while creating trigger upon inert getting error on end - oracle

CREATE OR REPLACE TRIGGER PEN_DELTA
BEFORE INSERT ON PS_GP_RSLT_DELTA
FOR EACH ROW
WHEN (NEW.FORWARD_IND = 'Y' AND NEW.GP_PAYGROUP = 'PG PEN')
BEGIN
FORWARD_IND = 'N';
END;
giving error on
END;
line. is anything I need to change

Related

WHEN-BUTTON-PRESSED trigger raise unhendled exception ORA-01407

I am new in PL SQL and I am trying to resolve problem with copy-past data from .CVS file to database
I create a small application which will take data from .CVS and past it to database.
I create a method, but after I compile it's writtend Successfully compiled
But when I run form I get error
WHEN-BUTTON-PRESSED trigger raise unhendled exception ORA-01407
Does anyone know what this means since I google it and could not find anything ?
I would be very thankfull
declare
import_file text_io.file_type;
import_file_name varchar2(1000);
import_log_file text_io.file_type;
import_log_file_name varchar2(1000);
vec_importovano number;
brojac number;
brojac_redova number;
linebuf varchar2(5000);
p_rbr varchar2(4);
p_polica varchar2(20);
p_banka varchar2 (20);
p_kontakt varchar2(20);
kraj_fajla number;
begin
import_file_name := :Global.Lokacija_prenosa||:import.naziv_fajla||:Global.Ekstenzija_prenosa;
import_file := text_io.fopen(import_file_name,'r');
--p_rbr := 100000;
delete from zivot_trajni_nalog_ponude where banka is not null;
commit;
kraj_fajla := 0;
while kraj_fajla = 0 loop
begin
text_io.get_line(import_file, linebuf);
if brojac_redova>=2 then
if length(linebuf)>100 then
p_rbr:=substr(linebuf, 1, instr(linebuf,';',1,1)-1);
p_polica:=substr(linebuf, instr(linebuf,';',1,1)+1, instr(linebuf,';',1,2) - instr(linebuf,';',1,1)-1);
p_banka:=substr(linebuf, instr(linebuf,';',1,2)+1, instr(linebuf,';',1,3) - instr(linebuf,';',1,2)-1);
p_kontakt:=substr(linebuf, instr(linebuf,';',1,3)+1, instr(linebuf,';',1,4) - instr(linebuf,';',1,3)-1);
select count(*)
into vec_importovano
from ZIVOT_TRAJNI_NALOG_PONUDE
where broj_police=p_polica and p_rbr=redni_broj;
if vec_importovano=0 then
insert into ZIVOT_TRAJNI_NALOG_PONUDE values(p_rbr, p_polica, p_banka, p_kontakt);
commit;
end if;
end if;
end if;
EXCEPTION WHEN NO_DATA_FOUND THEN kraj_fajla := 1;
end;
end loop;
update zivot_trajni_nalog_ponude set redni_broj = p_rbr;
commit;
text_io.fclose(import_file);
message('Zavrseno prepisivanje fajla');
end;
The error you got (ORA-01407) means that you are trying to update a column (which is set to NOT NULL) with a NULL value. That won't work. For example:
SQL> create table test (id number not null);
Table created.
SQL> insert into test (id) values (100);
1 row created.
SQL> update test set id = null;
update test set id = null
*
ERROR at line 1:
ORA-01407: cannot update ("SCOTT"."TEST"."ID") to NULL
SQL>
The only UPDATE in your code is this:
UPDATE zivot_trajni_nalog_ponude SET redni_broj = p_rbr;
Apparently, p_rbr is NULL, redni_broj won't accept it and you got the error.
What to do? Debug your code and see why p_rbr doesn't have a value. A simple "solution" might be
IF p_rbr IS NOT NULL
THEN
UPDATE zivot_trajni_nalog_ponude
SET redni_broj = p_rbr;
END IF;
Also, although not related to your problem: don't COMMIT within a loop.
ORA-01407 occurs as you are trying to update/Insert a column to NULL
when the column does not accept NULL values.
To find all the "not null" columns in table ZIVOT_TRAJNI_NALOG_PONUDE, Please check the DDL of the table.

Check record exist before update

I want to update a table but I want to check if the record exist. If not then throw an exception. In the C# application I pass the parameters and execute the command by the following.
procedure usp_update_example
(
p_id in mydb.member.idn_member%type,
p_idn_person in mydb.member.idn_person%type,
p_ind_rep in mydb.member.ind_rep%type
)
as
v_exist pls_integer := 0;
v_step varchar2(250);
v_exception_not_exist exception;
begin
v_step := 'Check for record ' || p_id;
select count(1)
into v_exist
from mydb.member
where idn_member = p_id;
if v_exist = 0 then
raise v_exception_not_exist;
end if;
if (v_exist > 0) then
v_step := 'Update table :' || p_id;
update mydb.member
set
idn_person = p_idn_person,
ind_rep = p_ind_rep
where idn_member = p_id;
end if;
exception
when v_exception_not_exist then
Raise_application_error(-20001, 'Not exist');
end usp_update_example;
However even my condition is right, I do have the record existing in the table. I always get Not exist exception. If I don't use if v_exist = 0 and use WHEN NO_DATA_FOUND THEN. Then everything is fine.
I am not sure where is wrong.
Your code seems to be fine. Looks like this issue is related to some uncommitted data - you see the record in the session where you inserted it and you don't see it in C# session since the record is not committed yet. Hence, C# session generates exception.
What I would suggest re the procedure code is to make it more compact.
Something like the following:
...
begin
update mydb.member
set idn_person = p_idn_person,
ind_rep = p_ind_rep
where idn_member = p_id;
if SQL%ROWCOUNT = 0 then
raise_application_error(-20001,'Not exist');
end if;
end;

Oracle SP2-0552: Bind variable "NEW" is not declared

I am trying to create a simple trigger but I got below error. I searched on the internet but could not find the solution. Could you help me on this issue?
create trigger ProcessTigger before insert on T039
for each row
declare consecutivo int; idconsecutivo int; maxconsecutivo int;
begin
select t326c004 into consecutivo from T326 where t326c003 = 'T039' and t326c002 = :new.t039c004;
if consecutivo is not null
then
consecutivo :=consecutivo+1;
select t326c001 into idconsecutivo from T326 where t326c002 = :new.t039c004 and t326c003=T039;
update T326 set t326c004 = consecutivo where t326c001=idconsecutivo and t326c003=T039;
else
select max(t039c003) into maxconsecutivo from T039 where t071c002=:new.t039c004;
if maxconsecutivo is not null
then consecutivo := maxconsecutivo+1;
else consecutivo:=1;
end if;
insert into T326
(t326c002,t326c003,t326c004)values(:new.t039c004,'T039',consecutivo);
end if;
end;
ERROR:
SP2-0552: Bind variable "NEW" is not declared.
If this is your idea of "a simple trigger" then I wonder what a complicated one would like?
It seems likely that the SP2-0552 error is because you're running a script with rogue newlines without setting SQLBLANKLINES.
But once you've fixed the syntax errors you'll find your trigger won't run due to a mutating table error. We can't select in a trigger from the underlying table because the state is indeterminate. So this is wrong:
select max(t039c003) into maxconsecutivo
from T039
where t071c002=:new.t039c004;
You need to find a different way of implementing whatever business rule that is supposed to do.
The use of triggers for such function of dispensing IDs is not safe. Remember there could be more than an insert that will race to get the next 'consecutivo' and get the same ID
Also, the issue of mutating table, where you cannot select from the same table in a row-level trigger.
In addition to that, you have syntax error in lines like the below where you're not enclosing T039 with quotes!
select t326c001 into idconsecutivo from T326 where t326c002 = :new.t039c004
and t326c003=T039;
update T326 set t326c004 = consecutivo where t326c001=idconsecutivo and
t326c003=T039;
I suspect the error that you get is due to an invalid reference to a column (when using :new)
You can try the following trigger and function:
Create an autonomous_transaction function to insert the initial "consecutivo"
In the trigger, start with insert (calling the function), and if not creating a record, then update
create or replace
trigger processtrigger
before insert on t039
for each row
declare
v_id number;
begin
-- start with insert calling the function
if f_create_new_con(:new.t039c004) = 0 then
update t326 set t326c004 = t326c004 + 1 -- this is safe since it will take the last committed t326c004 and increase it
where t326c003 = 'T039'
and t326c002 = :new.t039c004;
end if;
end;
/
create or replace
function f_create_new_con(p_value in number) return number
is
pragma autonomous_transaction;
begin
insert into t326 (t326c002, t326c003, t326c004)
select p_value, 'T039', (select nvl(max(t039c003), 0) + 1 from t039 where t071c002 = p_value)
from dual
where not exists (select 1 from t326 where t326c002 = p_value and t326c003 = 'T039');
-- if no insert then return 0 to update
if (sql%rowcount = 0) then
return 0;
else
return 1;
end if;
end;
/

Creating Trigger is giving me Error

I have created a Trigger to insert values in table. But while compiling it, I am getting error as
Error(10,80): PL/SQL: ORA-01745: invalid host/bind variable name
Below is my trigger
CREATE OR REPLACE TRIGGER TR_UPDATE_FR_LOGTYPE1
AFTER INSERT OR UPDATE ON LOGSAPDEALSLIPFUNDREQINTGRTN
FOR EACH ROW
BEGIN
IF (:NEW.RESPONSESTRING LIKE '%Record already exists%'
AND:NEW.LOGTYPE = 'ServiceFault')
THEN
--:NEW.LOGTYPE := 'Success';
Insert into LOGSAPDEALSLIPFUNDREQINTGRTN (LOGTYPE) values (:NEW.LOGTYPE := 'Success');
END IF;
END;
You simply need to edit this way:
...
Insert into LOGSAPDEALSLIPFUNDREQINTGRTN (LOGTYPE) values ('Success');
...

Trigger, at line 1 ORA 04098

Hi im setting up some triggers and i cant get past this error
SET SERVEROUTPUT ON
CREATE OR REPLACE TRIGGER trigger2
BEFORE INSERT ON new_donation
FOR EACH ROW
--WHEN (new.contamt <= 10)
DECLARE
v_idno VARCHAR2(5);
v_driveno VARCHAR2(3);
v_contdate DATE;
v_contamt NUMBER(6,2);
BEGIN
SELECT IDNO, DRIVENO, CONTDATE, CONTAMT INTO v_idno, v_driveno, v_contdate, v_contamt
FROM OLD_DONATION2
WHERE IDNO = :new.idno;
IF :new.contamt < 50 THEN
RAISE_APPLICATION_ERROR (-20001, 'CONTRIBUTION TOO LOW FOR '
|| :new.idno || ' ' || :new.contamt);
END IF;
END;
/
SET SERVEROUTPUT OFF
another part
DECLARE
v_idno new_donation.idno%TYPE := '&in_idno';
v_driveno new_donation.driveno%TYPE := '&in_driveno';
v_contdate new_donation.contdate%TYPE := '&in_contdate';
v_contamt new_donation.contamt%TYPE := &in_contamt;
BEGIN
INSERT INTO new_donation
VALUES (v_idno, v_driveno, v_contdate, v_contamt);
END;
/
Im getting this error when I insert values at trigger2.
ERROR at line 1: ORA-04098: trigger 'XXXXXXXXX.NEW_DONATION' is
invalid and failed re-validation ORA-06512: at line 7
All im trying to do is insert some values input by the user to this new table, which is empty.
Also when donation amount is < 10 I want a error out.
Follow these steps.
Run this query select status from all_objects where object_name = 'TBL_USER_TRIGGER' and object_type = 'TRIGGER';
If the status is invalid run alter trigger trigger2 compile;
Then run show errors
It will throw the errors for you. You need to fix those.

Resources