Link to home
Start Free TrialLog in
Avatar of raana jella
raana jella

asked on

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

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.
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)...);
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

>>... 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
-->>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
>>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.
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.
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.
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.