Solved

Nesting an anonymous block

Posted on 2003-11-18
5
570 Views
Last Modified: 2013-12-12
I have a sql script that is structured like this:

DECLARE
BEGIN
OPEN CURSOR
LOOP
FETCH CURSOR
IF
  IF
   BEGIN
    SELECT INTO
    SELECT INTO
    INSERT
    UPDATE
    INSERT
 EXCEPTION
  dup_val
  rec not found
  END
 END IF
~~bunch of assignments and logic
ELSEif
 INSERT
ELSE
 NULL
END IF
END LOOP
 IF
   BEGIN
    SELECT
    SELECT
    INSERT
    UPDATE
    INSERT
 EXCEPTION
  dup_val
  no_data
  END
END IF
CLOSE CURSOR
COMMIT
END


I know this isn't very clean so I apologize in advance. In the section where I say logic and assignments I want to insert a little bit of code...

If  "thisvalue" THEN
   makethisassignment
ELSE
  SELECT into thisthing WHERE
    these conditions are met
END IF

EXCEPTION HANDLING

I can't get this to work. I tried making it an anonymous block by wrapping it in BEGIN and END. But it's having some weird interaction with the loop logic. Like the LOOP doesn't want the exception handling in it. I'd really appreciate any help on how to plug this into my existing script.

Thanks,

Rebecca

p.s.

If someone is interested in helping me with this I'd be happy to post the actual code that I'm working with. The main script is 166 lines.

0
Comment
Question by:rebeccap
5 Comments
 
LVL 5

Expert Comment

by:gmyers
ID: 9783796
This routine works fine.
What sort of behaviour are you getting (eg compilation error, never-ending loop ?)

DECLARE
 cursor c_1 is select 1 from dual;
 v_num number(1);
BEGIN
  OPEN c_1;
  LOOP
    FETCH c_1 into v_num;
    IF C_1%FOUND then
      dbms_output.put_line('a');
      IF v_num = 1 then
       BEGIN
        dbms_output.put_line('b');
        SELECT 2 INTO v_num from dual;
        SELECT 3 INTO v_num from dual;
    --    INSERT
    --    UPDATE
    --    INSERT
       EXCEPTION
--        when dup_val then null;
        when no_data_found then null;
       END;
      END IF;
      begin
        dbms_output.put_line('c');
        v_num := 9;
      exception
        when value_error then
          raise_application_error(-20001,'Bad');
      end;
    ELSIF c_1%NOTFOUND THEN
     dbms_output.put_line('d');
             exit;
     --INSERT
    ELSE
     dbms_output.put_line('e');
     NULL;
    END IF;
  END LOOP;
  dbms_output.put_line('f '||v_num);
  IF v_num < 10 then
       dbms_output.put_line('g');
       BEGIN
        SELECT 4 INTO v_num from dual;
        SELECT 5 INTO v_num from dual;
    --    INSERT
    --    UPDATE
    --    INSERT
       EXCEPTION
--        when dup_val then null;
        when no_data_found then null;
       END;
       dbms_output.put_line('h');
  END IF;
  CLOSE c_1;
  dbms_output.put_line('i');
  COMMIT;
END;


 
0
 

Author Comment

by:rebeccap
ID: 9792184
Thank you very much for your reply. I have gotten the script working. I believe the problem is that I was positioning my BEGIN in the wrong place. I was enclosing the IF statement in BEGIN and END. Once I wrapped the Select statement in the ELSE clause of the statement in BEGIN and END the errors went away.

I'm a little unclear on the syntax for anonymous blocks. It seems as though you need to keep everything directly anchored to a select statement. Is that correct?

Thanks again.

Rebecca
0
 
LVL 3

Expert Comment

by:jaramill
ID: 9855037
Anonymous blocks either start as

BEGIN

.....

END;

or

DECLARE

  local variables

BEGIN

  code....

EXCEPTION

END;

If you have a SELECT statement embedded (i.e. implicit SQL) then you have to SELECT columns INTO local variables and you should preprare for ORACLE exceptions being raised (i.e. TOO_MANY_ROWS, or NO_DATA_FOUND) and then handle them in the EXCEPTION area, then exit the anonymous block, and continue processing in the calling block (which could be either another anonymous block or a named block (i.e. a stored procedure or function).

Gio
0
 
LVL 22

Expert Comment

by:Helena Marková
ID: 10200467
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ/Refund.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Henka
EE Cleanup Volunteer
0
 

Accepted Solution

by:
SpazMODic earned 0 total points
ID: 10266106
PAQed, with points refunded (500)

SpazMODic
EE Moderator
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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

914 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

19 Experts available now in Live!

Get 1:1 Help Now