Solved

Oracle trigger

Posted on 2013-01-17
15
622 Views
Last Modified: 2013-01-17
I have never created a trigger before in Oracle. I need to update a column when a record gets inserted or updated to be Upper Case. This is what I want to run on Insert/Update

UPDATE OMNI_COMMOM_LOOKUP SET LOOKUP_VALUE_TEXT=UPPER(LOOKUP_VALUE_TEXT) WHERE LOOKUP_KEY LIKE 'TRANSIT_TYPE_%';

I want this done After Update/Insert but when I try to create it I get a message that says it compiles but with warnings. Im using TOAD but cant figure it out. I put the Update statement in the Body tab and click the appropriate check boxes for Insert Update and After but I still cant figure it out.

Please walk me through step by step to create this trigger

Thanks
0
Comment
Question by:jknj72
  • 9
  • 6
15 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38787817
Untested, just typed in but try something like:

create or replace trigger OMNI_COMMOM_LOOKUP_TRIG
after insert or update on OMNI_COMMOM_LOOKUP
for each row
begin
:new.LOOKUP_KEY  := upper(:new.LOOKUP_KEY);
end;
/
0
 

Author Comment

by:jknj72
ID: 38787879
I ran what you sent me I got the following error.

create or replace trigger ANET.TRG_OMNI_COMMOM_LOOKUP
after insert or update on ANET.OMNI_COMMOM_LOOKUP
for each row
begin
:new.LOOKUP_KEY  := upper(:new.LOOKUP_KEY);
end;
Error at line 2
ORA-00942: table or view does not exist


Also, Im trying to Update the LOOKUP_VALUE_TEXT field not LOOKUP_KEY. No idea why this is happening, any idea?
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38787904
>>trying to Update the LOOKUP_VALUE_TEXT field not LOOKUP_KEY

Then change the column name.

>>ORA-00942: table or view does not exist

Either the user you are logged into the database with doesn't have permissions to see ANET.OMNI_COMMOM_LOOKUP or the table really doesn't exist.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:jknj72
ID: 38788155
hahah, nice answser!!! I just wanted to make sure that I was updating the correct column...

Anyway, I got this error now.

Compilation (1: 26): ORA-04084: cannot change NEW values for this trigger type
0
 

Author Comment

by:jknj72
ID: 38788158
This too when using TOAD tocreate trigger....

Error at line 3
ORA-04082: NEW or OLD references not allowed in table level triggers
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38788165
>>ORA-04082: NEW or OLD references not allowed in table level triggers

Make sure you have 'for each row'.

>> Compilation (1: 26): ORA-04084: cannot change NEW values for this trigger type

Change it to a before update/insert trigger.
0
 

Author Comment

by:jknj72
ID: 38788274
ok that worked. I just need to script this out for the DBAs. When I script other objects I always include a Synonym and Grants, do I have to worry about this with a Trigger?
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38788289
>>do I have to worry about this with a Trigger?

The only think I can think of and I'm not even sure of that is if, for some odd reason, the trigger is on tables that aren't in the same schema as the trigger and that schema doesn't already have the necessary rights.
0
 

Author Comment

by:jknj72
ID: 38788297
Also, Where is the Where Clause that I need in the Trigger Update?
0
 

Author Comment

by:jknj72
ID: 38788300
ok Thanks. I just need to set the Where clause and Im good to go!!
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38788311
>>Also, Where is the Where Clause that I need in the Trigger Update?

What 'where' clause?  A trigger fires 'for each row' it is passed.  This is determined by the DML statement issued against the base table.
0
 

Author Comment

by:jknj72
ID: 38788318
ok maybe im a little confused. I only want this to run if the
WHERE LOOKUP_KEY LIKE 'TRANSIT_TYPE_%';


This table is used for multiple apps and I only want this to run for these KEYS
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 38788342
That where clause is part of the update statement that fires the trigger.

As you saying you only want to make LOOKUP_VALUE_TEXT upper case if LOOKUP_KEY LIKE 'TRANSIT_TYPE_%'?

If so, just add an if statement to the trigger.

...
if :new.LOOKUP_KEY LIKE 'TRANSIT_TYPE_%' then
   :new.LOOKUP_VALUE_TEXT:= upper(:new.LOOKUP_VALUE_TEXT);
end if;
...
0
 

Author Comment

by:jknj72
ID: 38788398
ok that worked great, thanks for the help
0
 

Author Closing Comment

by:jknj72
ID: 38788403
thanks
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read about achieving the basic levels of HRIS security in the workplace.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question