Oracle ---- stored procedures and functions

Stored procedure
stored process model parameters include IN, OUT, IN OUT.

IN (default parameter mode): indicates when not stored procedure call, the argument value is passed to the parameter; parameter role played variable, only read the parameter, and this parameter can not be changed. IN parameter may be a variable or an expression.
OUT: indicates when the stored procedure is called, the argument value is ignored; parameter acts uninitialized PL / SQL variables, an initial parameter value is NULL, can read / write operation, after the end of the stored procedure call , parameter values are given argument. OUT parameters can only be a variable, not a constant or expression.
IN OUT indicates when the stored procedure is called, the parameter is passed to the parameter value. Parameter acts initialized PL / SQL variables, readable and writable. IN OUT mode parameter can be variable, not a constant or expression.
Use OUT, IN OUT mode when the parameter only when the program ends normally only the parameter values passed to the argument.
For example:

Create or Replace Procedure proc_divide
(num1 in OUT Number, num2 in OUT Number) IS
R1 Number;
R2 Number;
the begin
R1: = the trunc (num1 / num2);
R2: = MOD (num1, num2);
num1: = R1;
num2 : = R2;

Exception
When the then ZERO_DIVIDE
DBMS_OUTPUT.PUT_LINE ( 'dividing the denominator can not be zero');
When the then Others
DBMS_OUTPUT.PUT_LINE ( 'error run using the cursor!');
End proc_divide;

SET ON SERVEROUTPUT
DECLARE
N1 Number: & N1 =;
N2 number: & = N2;
the begin
proc_divide (N1, N2);
DBMS_OUTPUT.PUT_LINE ( 'a result of the division of two numbers:' || N1);
DBMS_OUTPUT.PUT_LINE ( 'modulo result is:' | | N2);
End;
function
(1) Create Create a stored procedure similar function, except that the function returning a display.

(2) did not declare the function key in the creation process, but rather to use as is or keyword instead.

(3) function has to return: return datatype.

(4) function is not generally performed DML (Data Manipulation Language - insert, delete, update) operation.

 

For example: stored procedure call function

- Function increaseSalary ()
Create or Replace function increaseSalary (theIncome in Number) return VARCHAR2
AS
theMessage VARCHAR2 (50);
the begin
IF theIncome <= 4000 the then
theMessage: = 'low income, increased wages% 10';
ELSIF theIncome <= the then 8000
theMessage: = 'low income, increased wages%. 5';
ELSIF theIncome <= 20000 the then
theMessage: = 'income generally increased wages% 2';
the else
theMessage: = 'high income, salary increase of 1%';
IF End;
return theMessage;
End;
- stored procedure
Create or Replace procedure getEmpInfo
(OUT employees.employee_id% in theID type,
theName% employees.first_name OUT type,
theSalary% employees.salary OUT type,
theMessage OUT VARCHAR2)
is
begin
select employee_id,first_name,salary,increaseSalary(salary)
into theId,theName,theSalary,theMessage
from employees
where employee_id = theId;

exception
when NO_DATA_FOUND then
dbms_output.put_line('没有该员工信息');
end;


set serveroutput on ;
declare
theId employees.employee_id%type:=&theId;
theName employees.first_name%type;
theSalary employees.salary%type;
theMessage varchar2(50);
begin
getEmpInfo(&theId,theName,theSalary,theMessage);--输入员工id
dbms_output.put_line('ID为:'||theId||'的员工,名字为'||theName
||', 收入为'||theSalary||','||theMessage);
end;

--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/10992432.html