Solved

Trigger, update on insert

Posted on 2014-02-04
12
582 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 77

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with duplicate records in Oracle query 16 40
what privileges needed for S2 for this function (Oracle 12c)? 3 23
update statement in oracle 9 29
oracle collections 2 22
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

828 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