Solved

Help with AFTER INSERT trigger

Posted on 2014-09-17
9
315 Views
Last Modified: 2014-12-03
Gurus,

I wrote a before insert trigger that isn't working.

create or replace
TRIGGER katvf7c_to_katimpc before
insert on KAT for each row
begin
:new.KATIMPC := :new.KATPF7C;
end;

What is the best way to convert this to an AFTER INSERT trigger as I can use the :new option?  Thanks in advance!
0
Comment
Question by:xbox360dp
  • 4
  • 3
  • 2
9 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 40328577
you can use :new in a before row trigger

your syntax is valid, what isn't working about it?

what are you trying to do?
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40328583
Instead of changing it, fix what isn't working.

This works for me:
drop table tab1 purge;
create table tab1(col1 char(1), col2 char(1));


create or replace trigger tab1_trig
before insert on tab1
for each row
begin
	:new.col2 := :new.col1;
end;
/

show errors

insert into tab1(col1) values('a');
commit;

select * from tab1;

Open in new window

0
 

Author Comment

by:xbox360dp
ID: 40328621
It's something funky through our application. If I insert the record directly through the database the trigger works fine.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40328636
Inserting into a table with a trigger and not having the trigger fire is not a feature of a 'funky application'.  It is a pretty huge bug/hole in the database.

The trigger as written will work.  Something isn't right.  I would dig deeper.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 500 total points
ID: 40328641
The trigger is fine "as is" - what is the application doing  that makes you think the trigger is not working?

Is the application doing direct path inserts? or updating the table after an insert so as to override the trigger's data change?


Just to follow up on your original question - trying to use an AFTER ROW trigger to change a value won't work.  You can reference the new value, but you can't change it - because it's too late at that point.

By definition of an AFTER trigger: the insert is already done, so the value is fixed.
0
 

Accepted Solution

by:
xbox360dp earned 0 total points
ID: 40328681
Made the following change and it works now. We must be doing an update as well!

create or replace
TRIGGER katvf7c_to_katimpc before
insert or update on KAT for each row
begin
:new.KATIMPC := :new.KATPF7C;
end;
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 40328699
if you have 2 columns that must always be equal, you might want to consider simply dropping the KATIMPC column and then recreating it as a virtual column.

Of course that will mean rearranging the order of columns in your table, so it might not be feasible.

Another option but a bit more cumbersome:  change the name of the table and create a view with the table's original name.  This will let you drop the physical column but recreate the logical column in its original position within the table.
0
 

Author Comment

by:xbox360dp
ID: 40474490
I've requested that this question be closed as follows:

Accepted answer: 0 points for xbox360dp's comment #a40328681

for the following reason:

This worked for me.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 40474491
Shouldn't a split be in order since you changed your trigger based on my comment about updates?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

896 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now