Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Advanced error checking using a trigger
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Advanced error checking using a trigger

Brian Peasland EXPERT RESPONSE FROM: Brian Peasland

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 04 June 2001

Hi, I have the following table:
create table test(sumdgt number(5));

In the above table, I should not enter the row whose total comes to 8.

For example,
It should accept,

insert into test values(21); insert into test values(11);
It should not accept,
insert into test values(71); (7+1=8) insert into test values(17000); (1+7+0+0+0)=8 insert into test values(26); (2+6=8)

Thanks.


>

The only way to handle this advanced type of error checking (or business rules) is to use a trigger. This trigger must then parse the number being inserted, character by character, and sum up the individual digits. Such a trigger might look like:

CREATE OR REPLACE TRIGGER test_no_8
BEFORE INSERT ON test
FOR EACH ROW
DECLARE
   i   NUMBER;
   sum NUMBER;
   len NUMBER;
BEGIN
   /* set sum of digits to zero */
   sum:=0;
   /* get length of string to be inserted */
   len:=LENGTH(:new.column_1);
   /* loop through string. Add individual digits */
   /* to the sum.                                */
   FOR i IN 1..len LOOP
         sum:=sum+SUBSTR(:new.column_1,i,1);
   END LOOP;
   /* If sum is 8, then raise an error. */
   /* otherwise, do nothing             */
   IF sum=8 THEN
      RAISE insert_error;

EXCEPTION
   WHEN insert_error THEN
      RAISE_APPLICATION_ERROR(-20001,'String digits equals 8');
END;

You should test this code as I just wrote it off the top of my head. I have not tested it myself, but it should work.

For More Information


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts