Oracle database to create stored procedures, functions, packages (six)

First, stored procedures

  1. Introduction stored procedure
    a, stored in Oracle AKA procedure subroutine;
    B, named PL / SQL block, compiled and stored in a database;
    each part C, stored procedure:

    声明部分;
    可执行部分;
    异常处理部分(可选)。
    

    d, stored classification procedure:

    过程-执行某些操作;
    函数-执行操作并返回值;
    
  2. Advantage of stored procedures
    a, modular: the program divided into logic module;
    B, reusability: may be any number of procedure calls;
    C, maintainability: simplify maintenance operations;
    D, security: set permissions, so data more secure.

  3. Syntax to create a stored procedure

--创建存储过程:
create [or replace] procedure 存储过程名称 [(参数名称 参数类型,参数名称 参数类型)]
is|as
局部变量声明区
begin
[可执行SQL语句;]
[exception
异常处理语句]
end 过程名称;

--调用存储过程
begin
过程名称[(参数名称 参数类型,参数名称 参数类型)];
end;

Examples:
in parameters stored procedure:

--3. 创建一个带参数的存储过程,根据制定的参数删除一个员工。
--创建存储过程:
create or replace procedure emp2(theEmpNo emp.empNo%type)
as
begin
delete from emp where empNo = theEmpNo;
dbms_output.put_line('删除成功!');
end;

--调用存储过程:
declare
theEmpNo emp.empNo%type;
begin
theEmpNo:='&请输入员工编号';
emp2(theEmpNo);
end;

out parameters stored procedure:

--4.创建一个带输出参数的存储过程,在这个过程中首先调用上面的那个存储过程,然后用输出参数返回剩下的员工人数
--创建存储过程:
create or replace procedure emp3(
theDeptNo emp.deptNo%type,   --员工编号
peopleNumber out number    --人数
)
as
begin
emp2(theDeptNo);
select count(*) into peopleNumber from emp;
dbms_output.put_line(peopleNumber);
end;

--调用存储过程:
declare 
theDeptNo emp.empNo%type;   --员工编号
peopleNumber number;    --人数
begin
theDeptNo:='&请输入员工编号';
emp3(theDeptNo,peopleNumber);
end;
  1. Independent transaction
    a, the independent Transaction Processing

    主事务处理启动独立事务处理;
    主事务处理启动独立事务处理;
    自主事务处理存储过程内的SQL操作;
    然后终止自主事务处理;
    然后终止自主事务处理。
    

    b, PRAGMA AUTO NO MOUS TRANSACTION tag storage for transaction processing procedure to customize
    c, wherein independent transaction:

    与主事务处理的状态无关;
    提交或回滚操作不影响主事务处理;
    自主事务处理的结果对其他事务是可见的;
    能够启动其他自主事务处理。
    

Second, the function

  1. Creating function syntax
grant or replace function 函数名称 [(参数名称 参数类型,参数名称 参数类型)] return 返回类型
is|as
[局部变量声明]
begin
执行语句;
return 返回值;
[exception 
异常处理;]
end;

example:

--2.定义一个函数,用来求一个数的N次幂的结果并返回,底数和幂数在调用函数的时候作为参数传递过来
--创建函数:
create or replace function power(
a int, --变量值
b int  --次幂
)
return int
as
c int;
begin
for i in 1..b
loop
c:=c*a;
end loop;
return c;
end;

--调用函数:
declare
a int;
b int;
c int;
begin
a:='&请输入一个数字';
b:='&请输入次幂';
c:=power(a,b)
dbms_output.put_line(c);
end;
  1. Defined functions and access functions
    a, defined function restrictions:

    形参不能是PL/SQL类型
    函数的返回类型也必须是数据库类型
    

    b, access functions in two ways:

    使用PL/SQL块
    使用SQL语句
    
  2. Stored procedures and functions

process function
Implemented as PL / SQL statements As part of the call expression
In the specification does not contain a clause RETURN It must be included in the specifications in the RETURN clause
No value is returned You must return a single value
RETURN statement may comprise, but with a different function, it can not be used to return the value It must contain at least one RETURN statement

Third, the package

  1. Create a header syntax
create or replace package 包名称
is|as
[定义存储过程;]
[定义函数;]
end 包名;
  1. The package body definition
create or replace package body 包名
is|as
实现包头中定义的过程或函数:
end 包名;
  1. example
--创建包头
create or replace package power_package
is
procedure addPower(powerName varchar2,powerPic varchar2,del number);   --向表中添加数据
function getSum(beginNumber int,endNumber int) return int;       --求数字a到数字b的和
end power_package;

--程序包主体定义
create or replace package body power_package
as
procedure addPower(powerName varchar2,powerPic varchar2,del number)
as
begin
insert into tb_power values(zb.nextval,powerName,powerPic,del);
end addPower;

function getSum(beginNumber int,endNumber int) return int
as
b int:=0;    --存储结果
begin
for i in beginNumber..endNumber
loop
b:=b+i;
end loop;
return b;
end getSum;
end power_package;

/* 添加权限数据 */
begin
power_package.addPower('VIP','sac/casca/aaa.png',null);
end;

/*求beginNumber到endNumber的和并返回*/
	--调用包中的函数:
		declare
		beginNumber int;
		endNumber int;
		a int;
		begin
		beginNumber:='&请输入一个起始数字';
		endNumber:='&请输入一个结束数字';
		a:=power_package.getSum(beginNumber,endNumber);
		dbms_output.put_line(a);
		end;
  1. Advantage of the package
    a, modular
    b, easier application design
    c, information hiding
    d, new features
    e, better performance

Fourth, the built-in package

Package Name Explanation
DBMS_ OUTPUT Processing PL / SQL block and debug information output routine
DBMS_ LOB LOB data providing function of the type of operation
DBMS_ XMLQUERY Provided to convert the data into XML type of function
DBMS RANDOM Providing a random number generator
UTL_ FILE With PL / SQL programs to read and write operating system text files
  1. DBMS_ OUTPUT
    Here Insert Picture Description
  2. DBMS_ LOB
    Here Insert Picture Description
    Here Insert Picture Description
  3. DBMS_ XMLQUERY
    Here Insert Picture Description
  4. DBMS RANDOM
    Here Insert Picture Description
    Here Insert Picture Description
  5. UTL_ FILE
    Here Insert Picture Description
    Here Insert Picture Description
Published 36 original articles · won praise 7 · views 2061

Guess you like

Origin blog.csdn.net/q_2540638774/article/details/103901255