Solved

Trigger, update on insert

Posted on 2014-02-04
12
577 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-
  • 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 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 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
 
LVL 76

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

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 76

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 76

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

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.

Join & Write a Comment

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now