Proc Cursors and exceptions

Source: Internet
Author: User
Tags throw exception
Steps to use for cursors:        1.   Declaration vernier                     cursor   cursor name   is   cursor execution Statement        2.   Open cursors                  open   cursor name        3.   Extract data, save in specified variable                 FETCH     cursor name  into   variable name &NB Sp      4.   Close cursors            close  -    cursor name to see how a certain cursor is defined     &NBSP ;     CURSOR   mycursor  -is                  select   *    from   tablename    where  ;         or defining parameters with cursors           cursor    mycursor (tid   number) & nbsp is                select   *   from     tablename    where  id=tid;       If the statement is not certain when the cursor is defined, it can also be in the open cursor is          type   mycursor   is   ref   cursor;        v_cursor    mycursor;       Open    v_cursor    for     ' SQL statement ' or string variable         &N Bsp     This allows dynamic execution of statements    cursor parameters will be passed in at open time   Example:         DECLARE

V_rec Temptab%rowtype;
Cursor MyCursor (CID number)
Is
SELECT * FROM Temptab
where id<=cid;
Begin
Open MyCursor (2);
While Mycursor%isopen loop isopen to determine if the cursor is open
Fetch mycursor into V_rec;
Ifmycursor%notfound then NotFound determine if there is data in the cursor
Exit
End If;
Dbms_output.put_line (v_rec.id| | ' '|| V_rec.name);
End Loop;
Close MyCursor; Close cursor
End

Abnormal:

Exception type name

Too_many_rows a variable but returns more than one data

Others all other exceptions

The error number of the binding exception

pragma exception_init (exception name, binding error number); When such an exception occurs after binding, the error number is bound.

Raise exception name manually throw exception

To see an example of an unusual use

Declare
V_rec Temptab%rowtype;
ABC exception; Custom exceptions
Pragmaexception_init (abc,-0001); is an exception binding error number
Begin
SELECT * into V_rec from Temptabwhere;
If 2>1 Then
Raise ABC; Throwing Exceptions manually
End If;
INSERT into Temptab Valuesv_rec;
exception
When Too_many_rows then an exception and then excepion the tag after the capture
Dbms_output.put_line (' too_many_rowsexception ');
When ABC then
Dbms_output.put_line (' abcexception ');
When others then
Dbms_output.put_line (' othersexception ');
End;--others to put in the last

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.