Trigger, update on insert

Hello Experts,

I need a way of updating a row after insert.

I have a table with 3 columns.. lets say

brand, product, item.

into this table, brand and product are being inserted.

I want that when a row is inserted into brand and product, for the item column to grab the first 7 characters from the brand column

eg..

INSERTED THROUGH APP

BRAND		PRODUCT			ITEM
-----------------------------------------------
CIGARETTES	8726141 - NEWPORTS


UPDATE BY TRIGGER
BRAND		PRODUCT			ITEM
-----------------------------------------------
CIGARETTES	8726141 - NEWPORTS	8726141

Open in new window

FutureDBA-Asked:
Who is Participating?
 
slightwv (䄆 Netminder)Connect With a Mentor Commented:
Try something like this (untested, just based on what you had posted):

create or replace trigger CREDIT_ITEM_UPDATE
BEFORE INSERT ON CREDIT_DETAIL
FOR EACH ROW
BEGIN
 :NEW.ITEM := substr(:new.descr,1,7);
END;
0
 
FutureDBA-Author Commented:
Would this work?

create or replace trigger CREDIT_ITEM_UPDATE
BEFORE INSERT ON CREDIT_DETAIL
FOR EACH ROW
BEGIN
 SELECT trim(' ' from substr(descr,1,7))
  INTO   :NEW.ITEM
  FROM   credit_items;

END;

Open in new window

0
 
FutureDBA-Author Commented:
no didnt work, any help would be appreciated.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
slightwv (䄆 Netminder) Commented:
I also see two tables in your example:  CREDIT_DETAIL and credit_items.

Are there two tables or just one?
0
 
FutureDBA-Author Commented:
the data being inserted into credit_details, is coming from credit_items.

now that you mention it, since the data will already be in credit_detail, i won't need to use credit_items
0
 
FutureDBA-Author Commented:
This worked,

but it is leaving white spaces in the begining and end. going to play with it for a bit to see if i can get rid of the whitespaces.


create or replace trigger CREDIT_ITEM_UPDATE
BEFORE INSERT ON CREDIT_DETAIL
FOR EACH ROW
BEGIN
 :NEW.ITEM := substr(:new.pname,1,8);
END;

Open in new window

0
 
slightwv (䄆 Netminder) Commented:
>> i won't need to use credit_items

It's up to you but we will need the specific requirements to come up with a working solution.

You can easily set up an insert into one table from an insert trigger on another.

If my example above doesn't get you what you need, please provide sample data and expected results.

For example:  are you sure it will ALWAYS be the first 7 characters?  Maybe the first set of numbers (regexp_substr(:new.descr,'^[0-9]+') )?

This would get: 123 - some item
0
 
FutureDBA-Author Commented:
figured out the whitespaces.. thanks dude slight,as always, extremely helpful
0
 
FutureDBA-Author Commented:
final trigger

create or replace trigger CREDIT_ITEM_UPDATE
BEFORE INSERT ON CREDIT_DETAIL
FOR EACH ROW
BEGIN
 :NEW.ITEM := trim(' ' from substr(:new.pname,1,8));
END;

Open in new window

0
 
slightwv (䄆 Netminder) Commented:
>>play with it for a bit to see if i can get rid of the whitespaces.

What whitespaces?

substr with 1,8 is first 8 characters.

a trim() will remove all leading and trailing spaces:
trim(substr(:new.pname,1,8))

Please post actual data and expected results.
0
 
FutureDBA-Author Commented:
For future people looking for a similar solution.

my data looked like this,
"  89155 - TOT YOG PEACH 5 OZ            "

I needed to extract the numbers only which could have been anywhere in first 8 characters.
0
 
slightwv (䄆 Netminder) Commented:
FYI:
regular expressions can do it all in 1 step:
select regexp_substr('  89155 - TOT YOG PEACH 5 OZ            ','[0-9]+[^0-9]') from dual;


You will need to test to see if the substr/trim is faster since regular expressions are an expensive operation.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.