Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simple Loop Help for no Data Found

WithnoeJul 6 2012 — edited Jul 9 2012
I am new to PL/SQL and am having problem trying to handle people that didn't enter a proper rec.BANNER_ID. In the begining part
  for rec in c1 loop
      select SPRIDEN_pidm into pidm 
        from SPRIDEN
       where SPRIDEN_ID = rec.BANNER_ID and SPRIDEN_CHANGE_IND is null; 
I get no data found when an id number is entered that doesn't exist in the spriden table. I want to be able have the people that didn't put in a proper rec.BANNER_ID to be in the records skipped count which is towards the end. I have tried many things but I don't seem to get anywhere. I am sure it is pretty simple. I think I need to add some kind of "*IF"*
statement before the section mentioned above. Here is the entire piece of code
--set serveroutput on format wraped;
set serveroutput on;
set verify off;
spool /u03/banjobs/LOCAL/student/scholar_insert.txt
Declare
   term_code  varchar2(6) := '&&TERM';
   cursor c1 is select * from ttubantemp.SXTSCHOLAR;
   Pidm  Number;
   so   varchar2(2);
   icnt  number := 0;
   cnt   number := 0;
   insert_cnt number := 0;
   update_cnt number := 0;
   exist_cnt  number := 0;
   skip_cnt   number := 0;
BEGIN
   dbms_output.enable (buffer_size => null);
   for rec in c1 loop
      select SPRIDEN_pidm into pidm 
        from SPRIDEN
       where SPRIDEN_ID = rec.BANNER_ID and SPRIDEN_CHANGE_IND is null;
       Select Count(*) Into Cnt
         From SPRCMNT
         Where Sprcmnt_Pidm = Pidm
         And Sprcmnt_Cmtt_Code = 'SO';
 --        and SPRCMNT_TEXT not like term_code;
      If Cnt > 0 Then
         Select Sprcmnt_Cmtt_Code Into So
           From Sprcmnt
          Where Sprcmnt_Pidm = Pidm
          and  SPRCMNT_CMTT_CODE = 'SO';
      Else
         so := null;
      end if;
--
      dbms_output.put_line('BANNER_ID = '||rec.BANNER_ID||
                           ' - PIDM = '||Pidm||
                           ' - Term = '||Term_Code||
                           ' - cnt = '||Cnt);
                      
        If Pidm Is Not Null
        then
         Select Count(*) Into Cnt 
           From Spriden
          Where Spriden_Pidm = Pidm
          And So Is Null;
         if cnt > 0 then
            dbms_output.put_line('   INSERTING');
           INSERT INTO sprcmnt(
              SPRCMNT_PIDM,
              SPRCMNT_CMTT_CODE,
              SPRCMNT_TEXT,
              SPRCMNT_TEXT_NAR,
              SPRCMNT_CONFIDENTIAL_IND,
              SPRCMNT_DATE,
              SPRCMNT_ORIG_CODE,
              SPRCMNT_ACTIVITY_DATE,
              SPRCMNT_CTYP_CODE,
              SPRCMNT_CONTACT_DATE,
              SPRCMNT_USER_ID,
              SPRCMNT_CONTACT_FROM_TIME,
              SPRCMNT_CONTACT_TO_TIME)
       VALUES (pidm,
              'SO',
              term_code,
              NULL,
              'N',
              Sysdate,
              'SCHL',
              SYSDATE,
              NULL,
              Null,
              'NEXTGEN',
              NULL,
              NULL);
            insert_cnt := insert_cnt + 1;
         else
          select count(*) into cnt 
           from sprcmnt
          Where Sprcmnt_Pidm = Pidm
           And So Is Not Null
           And Sprcmnt_Text Like '&&%TERM%';
            if cnt > 0 then
               dbms_output.put_line('   UPDATING');
               update sprcmnt
                  set SPRCMNT_TEXT = SPRCMNT_TEXT||' '||term_code
                where SPRCMNT_PIDM = pidm
                  and  SPRCMNT_CMTT_CODE = 'SO';
                update sprcmnt
                  set SPRCMNT_ACTIVITY_DATE = sysdate
                where SPRCMNT_PIDM = pidm
                  and  SPRCMNT_CMTT_CODE = 'SO';
                 Update Sprcmnt
                  set SPRCMNT_USER_ID = 'NEXTGEN'
                where SPRCMNT_PIDM = pidm
                  and SPRCMNT_CMTT_CODE = 'SO';
                update_cnt := update_cnt + 1;
             else
                dbms_output.put_line('   RECORD EXISTS');
                exist_cnt := exist_cnt + 1;
             end if;
         end if;
         icnt := icnt + 1;
      else
          dbms_output.put_line('SKIPPED BANNER_ID = '||rec.BANNER_ID);
          skip_cnt := skip_cnt + 1;
      end if;
   end loop;
   commit;
   dbms_output.put_line('Total Records    = '||icnt);
   dbms_output.put_line('Records Inserted = '||insert_cnt);
   dbms_output.put_line('Records Updated  = '||update_cnt);
   dbms_output.put_line('Records Existed  = '||exist_cnt);
   dbms_output.put_line('Records Skipped  = '||skip_cnt);
END;
/
spool off;
/
EXIT
Here is the results I get with 1 good id and one bad id

Error report:
ORA-01403: no data found
ORA-06512: at line 15
01403. 00000 - "no data found"
*Cause:
*Action:
BANNER_ID = T00001227 - PIDM = 1450 - Term = 201280 - cnt = 0
INSERTING

Commit

Here are the results with only one good id

anonymous block completed
BANNER_ID = T00001227 - PIDM = 1450 - Term = 201280 - cnt = 0
INSERTING
Total Records = 1
Records Inserted = 1
Records Updated = 0
Records Existed = 0
Records Skipped = 0

Commit

Edited by: Withnoe on Jul 6, 2012 2:27 PM
This post has been answered by AdamMartin on Jul 6 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 6 2012
Added on Jul 6 2012
4 comments
2,004 views