Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Orcle trigger

Status
Not open for further replies.

akutty

IS-IT--Management
Jul 7, 2004
31
GB
Hi All,

The trigger below gets compiled succesfully, however it doesnt work as expected, what we want is when count not = 0 then it shd raise the error. somehow its not working
please help
regards
Anil

create or replace trigger Tr_CheckSRID
before insert or update of SR_ID on DTS_T_VIC_VOLUME
for each row
declare
temp Varchar(2);
cursor c1 is select count(*) Countsr from DTS_T_VIC_VOLUME WHERE SR_ID not in ( select distinct SR_ID from T_SR_Salesperson_Details) ;
begin

for temp in c1
loop

if temp.Countsr <>0 then
RAISE_APPLICATION_ERROR (-20107,'SR ID doesnt exists in column T_SR_Salesperson_Details');
end if;
end loop;
Exception
when no_data_found then
RAISE_APPLICATION_ERROR (-20100,'No data found on table T_SR_Salesperson_Details');
end Tr_CheckSRID ;
 
Anil,

You said,
Anil said:
(The trigger) doesnt work as expected
...What does the trigger do? You raise the error, ('-20107'), but have no exception handler for that exception, therefore you probably see something like, "Unhandled user-defined exception...".

(I'm sorry if I cannot respond quickly beyond this post...I must go to a Utah Oracle Users Group conference presently. But I'm sure one of the other Tek-Tipsters can respond to your reply.)

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)

Do you use Oracle and live or work in Utah, USA?
Then click here to join Utah Oracle Users Group on Tek-Tips.
 
How about

Code:
create or replace trigger Tr_CheckSRID 
  before insert or update of SR_ID on DTS_T_VIC_VOLUME
  for each row
declare
temp number;
cursor c1 is select 1 from T_SR_Salesperson_Details 
where  SR_ID=:new.SR_ID;
begin

open c1;
fetch c1 into temp;

if c1%notfound then
  close c1;

  RAISE_APPLICATION_ERROR (-20107,'SR ID doesnt exists in column T_SR_Salesperson_Details');
else
  close c1;
end if;

end Tr_CheckSRID  ;

I'd better not to comment/correct your code :)

Another idea is if possible to create foreign key.

Regards, Dima
 
You have a "MUTATING TABLE" Error due to this cursor:

cursor c1 is select count(*) Countsr from DTS_T_VIC_VOLUME

You cannot issue DML on the table which fired the trigger.

[evil]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Anil,

change to something like this

Code:
create or replace trigger Tr_CheckSRID 
  before insert or update of SR_ID on DTS_T_VIC_VOLUME
  for each row
declare
temp number;
cursor c1 is select count(1) Countsr from T_SR_Salesperson_Details where SR_ID = nvl(:new.DTS_T_VIC_VOLUME.SR_ID, :old.DTS_T_VIC_VOLUME.SR_ID);
begin
temp := 0;
open c1;
fetch c1.countr into temp;
if temp <>0 then
RAISE_APPLICATION_ERROR (-20107,'SR ID doesnt exists in column T_SR_Salesperson_Details');
end if;
Exception
when no_data_found then
RAISE_APPLICATION_ERROR (-20100,'No data found on table T_SR_Salesperson_Details');
end Tr_CheckSRID  ;

This way you can avoid the possible mutating Error. And there is really no need of the Loop.

Let me know if this helps.

-Engi
 
Thanks a lot to all :). working fine now
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top