Solved

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

Posted on 2014-09-19
8
1,959 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 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
Comment Utility
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
Comment Utility
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>... 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
 
LVL 16

Expert Comment

by:Wasim Akram Shaik
Comment Utility
-->>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
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>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
Comment Utility
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
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
Comment Utility
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

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.

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

744 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

17 Experts available now in Live!

Get 1:1 Help Now