05 stored procedures, stored functions and packet

Stored procedure to perform specific operations, the completion of a function that may have input parameters and output parameters in out. Stored function that returns a result, the use of the head function return clause specifies the type of return used in the return clause operative to return the results. Package for organizing stored procedures, functions, and so, the packet is divided into two parts: the package specification, the package body.

1, system, and software constraints

  • win10
  • oracle11g
  • sql developer

2, the memory operation

  • 1 a memory write process to add data to the table
# 创建存储过程,没有参数
create or replace procedure pro_demo1 
is
begin
  insert into emp(empno,ename,sal) values (88,'wanhe',2000);
end;
# 调用存储过程
exec pro_demo1();  --exec是sqlplus命令
或者
call  pro_demo1(); --call是sql命令
  • 2 write a stored procedure to update the employee salary information by entering the names and salaries of employees
#创建存储过程,有两个入参
create or replace procedure pro_demo2 (name varchar2,newSalary number)
as
begin
  update emp set sal=newSalary where ename=name;
end;
# 调用存储过程
exec pro_demo2('SCOTT',9000);
  • 3 to write a stored procedure that returns the number of employees by the name reference to the participation of employees
# 有入参,有出参
create or replace procedure pro_demo3 (no in emp.empno%type,username out emp.ename%type)
is 
begin
  select ename into username from emp where empno=no;
end;
# 在块中调用这样的存储过程
declare
  v_no emp.empno%type;
  v_ename emp.ename%type;
begin
  v_no:=&empno;
  pro_demo3(v_no,v_ename);
  dbms_output.put_line('雇员姓名:'||v_ename);
end;

3, the operation function storage

  • 1 Create a memory function to participate in the 10, returns the result
# 给入参加10,返回结果
create or replace function fun_demo1(num number) return number
is
  v_num number(10);
begin
  v_num:=num+10;
  return v_num;
end;  
# 在块中调用存储函数
declare
  v_num number(10);
begin
  v_num:=fun_demo1(25);
  dbms_output.put_line('函数返回值为:'||v_num);
end;

4, operation of the package

# 创建一个包,只有声明没有实现
create or replace package pac_demo1
is
  procedure pro_demo8(name emp.ename%type,newSal in number);
  function fun_demo8(name varchar2) return number;
end; 
# 创建包体,完成实现
create or replace package body pac_demo1
is
  procedure pro_demo8 (name emp.ename%type,newSal in number)
  is
  begin
    update emp set sal=newSal where ename=name;
  end;
  function fun_demo8 (name varchar2) return number
  is
    v_sal emp.sal%type;
  begin
    select sal into v_sal from emp where ename=name;
    return v_sal;
  end;
end;
# 调用
exec pac_demo1.pro_demo8('SCOTT',1111);
select pac_demo1.fun_demo8('SCOTT') from dual;

These are among the oracle stored procedures, functions, and storage of the packages they are composed.

Guess you like

Origin www.cnblogs.com/alichengxuyuan/p/12576994.html