Solved

Trigger, update on insert

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
one-way data "masking" MD5 sql 26 145
SQL query of Oracle 10g database. 8 76
File generation using utl_file 4 45
SQL Retrieve Values 4 55
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…
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 how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Via a live example, show how to take different types of Oracle backups using RMAN.

919 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

23 Experts available now in Live!

Get 1:1 Help Now