Solved

Trigger to update a date column value after the insert on the same row Oracle 11g

Posted on 2014-09-19
8
2,123 Views
Last Modified: 2014-10-04
HI,
I need some help with trigger to update a date column value if it is blank after the insert of row on a table

Table : ASSET_LN

ASSET_ID (Key Field)
ASSET_DATE (Key)
LINE_NBR  (Key)
DESCR
PAY_DATE


I have a application where values are comming from external source, all values are provided by source system except pay date. So I need a trigger to default paydate to asset date when a row is inserted into ASSET_LN .

PAY_DATE update need to happen at the same time when a new row is inserted.

Thanks for your help in advance.
0
Comment
Question by:raana jella
  • 4
  • 3
8 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40333967
Try this:
create or replace trigger asset_ln_trig
before insert on asset_ln
for each row
begin
      :new.pay_date := :new.asset_date;
end;
/
0
 
LVL 23

Expert Comment

by:David
ID: 40334301
Avoid the trigger, simply INSERT the value into both columns.  In your scenario, the value is known from the external source.

... asset_date, pay_date VALUES ( TO_DATE(assetdt), TO_DATE (NVL (paydt,assetdt)...);
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40334708
>>... asset_date, pay_date VALUES ( TO_DATE(assetdt), TO_DATE (NVL (paydt,assetdt)...);

You sure you can do this?  I get:  ORA-00984: column not allowed here
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 16

Expert Comment

by:Wasim Akram Shaik
ID: 40336019
-->>You sure you can do this?  I get:  ORA-00984: column not allowed here

I too agree with dvz, there is no need for a trigger to do this task it can be done in the insert statement itself

An illustration of using nvl in conjunction with to_date has been done here.

http://sqlfiddle.com/#!4/d1d72/7
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40336571
>>An illustration of using nvl in conjunction with to_date has been done here.

That uses sysdate NOT an actual column.

Also, sysdate is already a date so there is no need to use TO_DATE on it.

For a more real-world simulation that shows the error I referenced above:
http://sqlfiddle.com/#!4/d1d72/8

To copy the value from one column to another column on insert, I don't know how to do it without a trigger.
0
 
LVL 23

Expert Comment

by:David
ID: 40336604
I frequently miss an author's nuances, but this one seems clear enough:
-- "all values are provided by source system except pay date" I take to mean the input is ETL rather than interactive.  If so, I can fill multiple columns with a static value.
--  "default paydate to asset date when a row is inserted"  neither SYSDATE, nor to_date(SYSDATE), are relevant.  However, I might ask the developer to consider including the APPS-style WHO columns on such financial content.

Therefore, and IMO, the trigger that SLIGHTWV provided is necessary for an interactive INSERT only.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40336614
If you have static values, then yes, just use the value twice.

I thought you were trying to reference the column name in the VALUES clause not a 'variable':
... TO_DATE (NVL (paydt,assetdt)...);

I missed that the columns had an '_' and the VALUES did not.
0
 
LVL 23

Expert Comment

by:David
ID: 40361279
I'm pleased to see we could assist.  Slightwv did concur that with a static data source, such as a list of values, the function I proposed in ID: 40334301 was a better solution.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

820 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