Oracle Stored Procedure Fundamentals

Source: Internet
Author: User
Tags case statement rowcount

    • Statement block definition:
SQL code
    1. Decalre
    2. --Variable declaration
    3. VAR1 number (2); --declaration only
    4. Var2 char (2): = ' 11 '; --Initialize at the same time as the declaration
    5. Begin
    6. --Statement
    7. End --end of statement block

    • If statement
SQL code
    1. If a = 1 or b = 2 Then
    2. elsif c = 3 Then
    3. Else
    4. End If;

    • Case statement

If the case statement is a branch control statement, the last closing sentence is the end case, and if it is a control statement in the SELECT statement, only the end is required.

SQL code
  1. Declare
  2. Num Number (10): = 1;
  3. Begin
  4. Case
  5. When num = 0 Then dbms_output.put_line ( ' zero ');
  6. When num = 1 then dbms_output.put_line ( ' one ');
  7. Else Dbms_output.put_line ( ' default ');
  8. End Case ;
  9. Case Num
  10. When 0 then dbms_output.put_line ( ' zero ');
  11. When 1 then dbms_output.put_line ( ' one ');
  12. Else Dbms_output.put_line ( ' default ');
  13. End Case ;
  14. End

    • For loop

There are two main uses for a for loop.


1. Cycle a Range
Format: For i in [start. end] loop ... end loop;

SQL code
    1. For i in 0..9 loop
    2. Dbms_output.put_line (' I: ' | | i);
    3. End Loop;


2. Traversing an implicit cursor

The benefit of an implicit cursor is that it does not need to be closed manually, facilitating

SQL code
    1. For Currow in (
    2. Select T.col1, T.col2
    3. From tableName T
    4. where ...
    5. ) loop
    6. If currow.col1 = 0 Then
    7. return; --Abort SP, return
    8. End If;
    9. End Loop;

    • While loop
SQL code
  1. isOK: = 9;
  2. While isOK >= 0 loop
  3. isOK: = isok-1;
  4. If isOK = 8 Then
  5. continue; --like the continue semantics of the programming language, skip the remaining statements of the current loop and return to the beginning of the loop
  6. End If;
  7. If isOK = 4 Then
  8. Exit --like the break semantics of programming languages, jump out of the loop
  9. End If;
  10. Dbms_output.put_line (' isOK: ' | | isok);
  11. End Loop;
  12. Dbms_output.put_line (' outside while loop. ');

    • Stored Procedure Definitions
SQL code
  1. Create or replace procedure sp_name (
  2. --entry, parameter list, comma delimited.
  3. UID in Varchar2, --not with length information
  4. StartDate in date, --second input parameter
  5. Defaultvar in varchar2 Default "", --defaults, if not passed, note the order of the parameters
  6. isOK out number, --Output parameter
  7. Result out VARCHAR2 --Second output parameter
  8. )
  9. As
  10. --variable declaration, with each declaration ending with a semicolon. Can be initialized at the same time as the declaration
  11. Var1 VARCHAR2 (11);
  12. VAR2 Number (2): = 123;
  13. Begin
  14. --string splicing with | |
  15. Dbms_output.put_line (' isOK: ' | |  ' abc ');
  16. --Call other stored procedures
  17. Sub_sp_name (param1, Prarm2, outParam1, outParam2);
  18. End --end of stored procedure

    • function definition
SQL code
  1. Create or replace function func (
  2. --entry, parameter list, comma delimited.
  3. UID in Varchar2, --not with length information
  4. StartDate in date, --second input parameter
  5. Defaultvar in varchar2 Default "", --defaults, if not passed, note the order of the parameters
  6. isOK out number, --Output parameter
  7. Result out VARCHAR2 --Second output parameter
  8. )
  9. Return number --Define the return type
  10. As
  11. --variable declaration, with each declaration ending with a semicolon. Can be initialized at the same time as the declaration
  12. Var1 VARCHAR2 (11);
  13. VAR2 Number (2): = 123;
  14. Begin
  15. --string splicing with | |
  16. Dbms_output.put_line (' isOK: ' | |  ' abc ');
  17. return ret_val;
  18. End

    • Similarities and differences between stored procedures and functions

1, the two definitions are similar, can be with input and output parameters.
2, the function has a return value, the stored procedure does not.
3, the function calls in the SELECT statement, and the stored procedure is not, can be called independently.

    • Cursor

an implicit cursor
The benefit of an implicit cursor is that it does not need to be closed manually, facilitating

SQL code
    1. For Currow in (
    2. Select T.col1, T.col2
    3. From tableName T
    4. where ...
    5. ) loop
    6. If currow.col1 = 0 Then
    7. return; --Abort SP, return
    8. End If;
    9. End Loop;

An explicit cursor

SQL code
  1. Declare
  2. isOK Integer;
  3. V_EVENT_ID number (10);
  4. V_isagain number (2);
  5. V_rate number (2);
  6. V_sender Char (one): = ' 13800138000 ';
  7. Cursor Cursorvar is a select event_id, Isagain, rate from call_event where sender = V_sender; --Declaring cursors
  8. Begin
  9. Open Cursorvar; --Open cursor
  10. Loop
  11. fetch Cursorvar into v_event_id, V_isagain, v_rate; --Take value
  12. Exit when Cursorvar%notfound; --Exits the loop when no records are recorded
  13. Dbms_output.put_line (v_event_id | | ', ' | | | v_isagain | |  ', ' | | | v_rate);
  14. end Loop;
  15. Close Cursorvar; --Close cursor
  16. --The properties of the cursor are:%found,%notfounrd,%isopen,%rowcount;
  17. --%found: Returns True when records have been retrieved
  18. --%NOTFOUNRD: Returns True when records are not retrieved
  19. --%isopen: Returns True when the cursor is open
  20. --%rowcount: Represents the number of records retrieved, starting from 1
  21. End

Cursors with parameters

SQL code
    1. Declare
    2. isOK Integer;
    3. V_EVENT_ID number (10);
    4. V_isagain number (2);
    5. V_rate number (2);
    6. V_sender Char (one): = ' 13800138000 ';
    7. Cursor Cursorvar (p_sender varchar2) is a select event_id, Isagain, rate from call_event where sender = P_sen Der --Declaring cursors
    8. Begin
    9. Open Cursorvar (V_sender);  --Open the cursor and pass the argument in parentheses.
    10. Loop
    11. fetch Cursorvar into v_event_id, V_isagain, v_rate; --Take value
    12. Exit when Cursorvar%notfound; --Exits the loop when no records are recorded
    13. Dbms_output.put_line (v_event_id | | ', ' | | | v_isagain | |  ', ' | | | v_rate);
    14. end Loop;
    15. Close Cursorvar; --Close cursor
    16. End

Oracle Stored Procedure Fundamentals

Related Article

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.