?
Solved

Trigger, update on insert

Posted on 2014-02-04
12
Medium Priority
?
585 Views
Last Modified: 2014-02-05
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

0
Comment
Question by:FutureDBA-
[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
  • 7
  • 5
12 Comments
 

Author Comment

by:FutureDBA-
ID: 39834512
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
 

Author Comment

by:FutureDBA-
ID: 39834519
no didnt work, any help would be appreciated.
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 total points
ID: 39834522
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 77

Expert Comment

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

Are there two tables or just one?
0
 

Author Comment

by:FutureDBA-
ID: 39834529
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
 

Author Comment

by:FutureDBA-
ID: 39834532
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
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39834539
>> 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
 

Author Comment

by:FutureDBA-
ID: 39834540
figured out the whitespaces.. thanks dude slight,as always, extremely helpful
0
 

Author Comment

by:FutureDBA-
ID: 39834542
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
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39834543
>>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
 

Author Comment

by:FutureDBA-
ID: 39834546
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
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39837665
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

752 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