- Statement block definition:
SQL code
- Decalre
- --Variable declaration
- VAR1 number (2); --declaration only
- Var2 char (2): = ' 11 '; --Initialize at the same time as the declaration
- Begin
- --Statement
- End --end of statement block
SQL code
- If a = 1 or b = 2 Then
- elsif c = 3 Then
- Else
- End If;
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
- Declare
- Num Number (10): = 1;
- Begin
- Case
- When num = 0 Then dbms_output.put_line ( ' zero ');
- When num = 1 then dbms_output.put_line ( ' one ');
- Else Dbms_output.put_line ( ' default ');
- End Case ;
- Case Num
- When 0 then dbms_output.put_line ( ' zero ');
- When 1 then dbms_output.put_line ( ' one ');
- Else Dbms_output.put_line ( ' default ');
- End Case ;
- End
There are two main uses for a for loop.
1. Cycle a Range
Format: For i in [start. end] loop ... end loop;
SQL code
- For i in 0..9 loop
- Dbms_output.put_line (' I: ' | | i);
- 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
- For Currow in (
- Select T.col1, T.col2
- From tableName T
- where ...
- ) loop
- If currow.col1 = 0 Then
- return; --Abort SP, return
- End If;
- End Loop;
SQL code
- isOK: = 9;
- While isOK >= 0 loop
- isOK: = isok-1;
- If isOK = 8 Then
- 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
- End If;
- If isOK = 4 Then
- Exit --like the break semantics of programming languages, jump out of the loop
- End If;
- Dbms_output.put_line (' isOK: ' | | isok);
- End Loop;
- Dbms_output.put_line (' outside while loop. ');
- Stored Procedure Definitions
SQL code
- Create or replace procedure sp_name (
- --entry, parameter list, comma delimited.
- UID in Varchar2, --not with length information
- StartDate in date, --second input parameter
- Defaultvar in varchar2 Default "", --defaults, if not passed, note the order of the parameters
- isOK out number, --Output parameter
- Result out VARCHAR2 --Second output parameter
- )
- As
- --variable declaration, with each declaration ending with a semicolon. Can be initialized at the same time as the declaration
- Var1 VARCHAR2 (11);
- VAR2 Number (2): = 123;
- Begin
- --string splicing with | |
- Dbms_output.put_line (' isOK: ' | | ' abc ');
- --Call other stored procedures
- Sub_sp_name (param1, Prarm2, outParam1, outParam2);
- End --end of stored procedure
SQL code
- Create or replace function func (
- --entry, parameter list, comma delimited.
- UID in Varchar2, --not with length information
- StartDate in date, --second input parameter
- Defaultvar in varchar2 Default "", --defaults, if not passed, note the order of the parameters
- isOK out number, --Output parameter
- Result out VARCHAR2 --Second output parameter
- )
- Return number --Define the return type
- As
- --variable declaration, with each declaration ending with a semicolon. Can be initialized at the same time as the declaration
- Var1 VARCHAR2 (11);
- VAR2 Number (2): = 123;
- Begin
- --string splicing with | |
- Dbms_output.put_line (' isOK: ' | | ' abc ');
- return ret_val;
- 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.
an implicit cursor
The benefit of an implicit cursor is that it does not need to be closed manually, facilitating
SQL code
- For Currow in (
- Select T.col1, T.col2
- From tableName T
- where ...
- ) loop
- If currow.col1 = 0 Then
- return; --Abort SP, return
- End If;
- End Loop;
An explicit cursor
SQL code
- Declare
- isOK Integer;
- V_EVENT_ID number (10);
- V_isagain number (2);
- V_rate number (2);
- V_sender Char (one): = ' 13800138000 ';
- Cursor Cursorvar is a select event_id, Isagain, rate from call_event where sender = V_sender; --Declaring cursors
- Begin
- Open Cursorvar; --Open cursor
- Loop
- fetch Cursorvar into v_event_id, V_isagain, v_rate; --Take value
- Exit when Cursorvar%notfound; --Exits the loop when no records are recorded
- Dbms_output.put_line (v_event_id | | ', ' | | | v_isagain | | ', ' | | | v_rate);
- end Loop;
- Close Cursorvar; --Close cursor
- --The properties of the cursor are:%found,%notfounrd,%isopen,%rowcount;
- --%found: Returns True when records have been retrieved
- --%NOTFOUNRD: Returns True when records are not retrieved
- --%isopen: Returns True when the cursor is open
- --%rowcount: Represents the number of records retrieved, starting from 1
- End
Cursors with parameters
SQL code
- Declare
- isOK Integer;
- V_EVENT_ID number (10);
- V_isagain number (2);
- V_rate number (2);
- V_sender Char (one): = ' 13800138000 ';
- Cursor Cursorvar (p_sender varchar2) is a select event_id, Isagain, rate from call_event where sender = P_sen Der --Declaring cursors
- Begin
- Open Cursorvar (V_sender); --Open the cursor and pass the argument in parentheses.
- Loop
- fetch Cursorvar into v_event_id, V_isagain, v_rate; --Take value
- Exit when Cursorvar%notfound; --Exits the loop when no records are recorded
- Dbms_output.put_line (v_event_id | | ', ' | | | v_isagain | | ', ' | | | v_rate);
- end Loop;
- Close Cursorvar; --Close cursor
- End
Oracle Stored Procedure Fundamentals