FutureDBA-
asked on
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..
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
ASKER
no didnt work, any help would be appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I also see two tables in your example: CREDIT_DETAIL and credit_items.
Are there two tables or just one?
Are there two tables or just one?
ASKER
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
now that you mention it, since the data will already be in credit_detail, i won't need to use credit_items
ASKER
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.
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;
>> 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
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,
This would get: 123 - some item
ASKER
figured out the whitespaces.. thanks dude slight,as always, extremely helpful
ASKER
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;
>>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.
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.
ASKER
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.
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.
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.
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.
ASKER
Open in new window