Solved

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

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

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 76

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 76

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error executing command from server 6 49
Need a replacement data type in Oracle 6 77
Oracle Pivot Question 8 58
form builder not starting 3 34
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

775 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