?
Solved

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

Posted on 2014-09-19
8
Medium Priority
?
2,452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
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 syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses

777 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