Summary:
Syntax and example:
1. Stored Procedure
Syntax for creating a stored procedure:
CREATE [or replace] PROCEDURE procedure_name
[(Parameter_list)]
{IS |}
[Local_declarations]
BEGIN
Executable_statements
[EXCEPTION
Exception_handlers]
END [procedure_name];
Procedure_name indicates the process name.
Parameter_list is the parameter list.
Local_declarations is a partial declaration.
Executable_statements is an executable statement.
Exception_handlers is an exception handling program.
Example 1: demonstrate the creation process (assign a default value to the IN parameter IN the parameter list, and do not assign the default value to the OUT and in out parameters)
Create or replace procedure find_emp (emp_no in number: = 7900)
As
Empname varchar2 (20 );
Begin
Select ename into empname from emp where empno = emp_no;
Dbms_output.put_line ('employee name is '| empname );
Exception
When no_data_found then
Dbms_output.put_line ('employee id not found ');
End find_emp;
Call process: EXECUTE procudure_name (parameters_list );
You can also call it in the process and directly write procudure_name without writing EXECUTE.
Example 2: demonstrate the process of creating an OUT Parameter
Create or replace procedure test (value1 varchar2, value2 out number)
Is
Identity number;
Begin
Select sal into identity from emp where empno = value1;
If identity <2000 then
Value2: = 1000;
Else
Value2: = 500;
End if;
End;
Call the process with the OUT parameter:
Declare
Value2 number;
Begin
Test ('20140901', value2 );
Dbms_output.put_line (value2 );
End;
Example 3: Create an in out Parameter
Create or replace procedure swap (p1 in out number, p2 in out number)
Is
V_temp number;
Begin
V_temp: = p1;
P1: = p2;
P2: = v_temp;
End;
Call the process with the in out parameter:
Declare
Num1 number: = 100;
Num2 number: = 200;
Begin
Swap (num1, num2 );
Dbms_output.put_line ('num1 = '| num1 );
Dbms_output.put_line ('num2 = '| num2 );
End;
Example 4: grant the execution permission of the process to another user
Grant execute on find_emp TO scott;
Grant execute on swap to public;
Grant the execution permission of the find_emp process to scott, and grant the execution permission of the swap process to all database users.
Delete PROCEDURE Syntax: drop procedure procudure_name;
2. Functions
The syntax for defining a function is as follows:
CREATE [or replace] FUNCTION function_name
[(Parameter_list)]
RETURN datatype
{IS |}
[Local_declarations]
BEGIN
Executable_statements
[EXCEPTION
Exception_handlers]
END [function_name];
Function_name is the name of the function.
Parameter_list is the parameter list.
Local_declarations is a partial declaration.
Executable_statements is an executable statement.
Exception_handlers is an exception handling program.
Note when using functions: the format parameter must only use the database type, rather than the PL/SQL type. The return type of the function must also be the database type.
Functions cannot be executed independently. They can only be called using SQL statements or PL/SQL blocks.
Example 5: demonstrate how to create a function
Create or replace function fun_hello
Return varchar2 is
Begin
Return 'Hello friend ';
End;
Call the function: select fun_hello from dual;
Function authorization: For details, see Example 4.
Delete FUNCTION: drop function function_name
Differences between processes and functions
Process Functions
Called as a part of the expression for PL/SQL statement execution
A return clause is not included in a specification and must contain a RETURN clause.
If no value is returned, a single value must be returned.
It can contain a RETURN statement, but unlike a function, it cannot be used to RETURN a value and must contain at least one RETURN statement.
3. Packages
Syntax for creating a package specification:
CREATE [or replace] PACKAGE package_name
IS |
[Public type and item declarations]
[Subprogram specifications]
END [package_name];
Package_name indicates the package name.
Public type and item declarations are declared types, constants, variables, exceptions, and cursors.
Subprogram specifications declare PL/SQL subprograms.
Example 6: demonstrate creating a package specification
Create or replace package pack_op is
Procedure pro_print_ename (id number );
Procedure pro_print_sal (id number );
Function fun_re_date (id number) return date;
End;
Syntax for creating a package subject:
CREATE [or replace] package body package_name
IS |
[Public type and item declarations]
[Subprogram bodies]
[BEGIN
Initialization_statements]
END [package_name];
Package_name indicates the package name.
Public type and item declarations are declared types, constants, variables, exceptions, and cursors.
Subprogram bodies is a common and private PL/SQL subroutine defined.
Example 7: Create a package subject
Create or replace package body pack_op is
Procedure pro_print_ename (id number) is
Name emp. ename % type;
Begin
Select ename into name from emp where empno = id;
Dbms_output.put_line ('employee name: '| name );
End pro_print_ename;
Procedure pro_print_sal (id number) is
Salary emp. sal % type;
Begin
Select sal into salary from emp where empno = id;
Dbms_output.put_line ('employee salary: '| salary );
End pro_print_sal;
Function fun_re_date (id number) return date is
Bedate emp. hiredate % type;
Begin
Select hiredate into bedate from emp where empno = id;
Return bedate;
End fun_re_date;
End pack_op;
Example 8: Call the process and function created in the package
Exec pack_op.pro_print_ename (7900 );
Exec pack_op.pro_print_sal (7900 );
Select pack_op.fun_re_date (7900) from dual;
Example 9: demonstrate the cursor in the package
Create a package specification
Create or replace package pack_emp is
Cursor cur_emp return emp % rowtype;
Procedure pro_cur;
End pack_emp;
Create a package subject
Create or replace package body pack_emp is
Cursor cur_emp return emp % rowtype is
Select * from emp;
Procedure pro_cur is
Rec_emp emp % rowtype;
Begin
Open cur_emp;
Loop
Fetch cur_emp into rec_emp;
Exit when cur_emp % notfound;
If rec_emp.sal <1000 then
Dbms_output.put_line ('employee salary: '| rec_emp.sal |', you need to redouble your efforts to raise the salary ');
Elsif rec_emp.sal> = 1000 and rec_emp.sal <2000 then
Dbms_output.put_line ('employee salary: '| rec_emp.sal |', general salary, strive for a department manager to do it ');
Else
Dbms_output.put_line ('employee salary: '| rec_emp.sal |', the salary is good, and a general manager is required ');
End if;
End loop;
End pro_cur;
End pack_emp;
Call the procedure in the package to call the cursor in the package
Exec pack_emp.pro_cur;
Example 10: The stored procedure returns the cursor's subroutine package (this package returns r_cur cursor)
Create or replace package SCOTT. pk_wt
Is
Type mytype is ref cursor;
Procedure p_wt (mycs out mytype );
End;
Create or replace package body scott. pk_wt
Is
Procedure p_wt (mycs out mytype)
Is
R_cur mytype;
Begin
Open r_cur for select * from emp;
Mycs: = r_cur;
End p_wt;
End pk_wt;
Query Information about processes, functions, and packages: USER_OBJECTS data dictionary View
Column object_name format a18
Select object_name, object_type from user_objects where object_type in ('Procedure ', 'function', 'package', 'package body ');
Note:
1. stored procedure parameters do not have a value range. in indicates input (only on the right of the equal sign) and out indicates output (only on the left of the equal sign ), in out indicates that this type can be assigned values or passed values;
Type can use the legal type in any Oracle.
2. The variable has a value range followed by a semicolon
3. Use the count (*) function to determine whether the operation record exists before determining the statement.
4. select... Into... Assign values to variables
5. If an exception is thrown in the code, use raise + Exception name.
6. When a select statement is used, the into field must be followed. In the cursor and loop, the into field must be followed.
7. You cannot create or replace a package.
8. Do not write in or out. The default value is in.