define:

  存储过程(Stored Procedure )是一组为了完成特定功能的SQL 语句 集,经编译后存储在数据库中。用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。存储过程是数据库中的一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程。存储过程是由流控制和SQL 语句书写的过程,这个过程经编译和优化后 存储在数据库服务器中,应用程序使用时只要调用即可。在ORACLE 中,若干个有联系的过程可以组合在一起构成程序包。

advantage:

1.存储过程只在创造时进行编译,以后每次执行存储过程都不需再重新编译,而一般SQL语句每执行一次就编译一次,所以使用存储过程可提高数据库执行速度。
2.当对数据库进行复杂操作时(如对多个表进行Update、Insert、Query、Delete时),可将此复杂操作用存储过程封装起来与数据库提供的事务处理结合一起使用。
3.stored procedure can  be used  many times  to reduce database  developer work task.
4.high safety,set a user to use specify stored procedure )可设定只有某用户才具有对指定存储过程的使用权。

1.basic syntax

create [or replace] procedure pro_name [parameter1[,parameter2]] is|as

begin

plsql_sentences;

[exception]

[dowith_sentences;]

end [pro_name];

note: bracket [] represent  can ignore, | represent need option either is or as.

2.exercise

1.compile

create or replace procedure pro_insertDept is
 begin
     insert into dept values(90,'market','fuzhou');
      commit;
      dbms_output.put_line('insert into successfully');
end pro_insertDept;
 /ou

note: if you come out a error can use show error command;

2.execute

preface:you should lauch result print out in following sentence.

set serveout on

next:you can use the below sentences execute procedure.

execute[exec] pro_insertDept;

3.invoking procedure in pl/sql program block.

begin

  pro_insertDept;

end;

3.stored parameter

1.Stroed procedure patameter contain  in,out ,in out  three variety model.

a.in model (default model)  example in the following code

CREAET OR REPLACE PROCEDURE pro_insertDept(
num_deptno in number,
var_ename in varchar2,
var_loc in varchar2
) AS
BEGIN
INSERT INTO dept VALUES(num_deptno,var_ename,var_loc);
commit;
END pro_insertDept;
/
begin
pro_insertDept(var_ename=>'purchase',num_deptno => ,var_loc => 'fz');
-- Open the cursor and loop through the records
FOR v_rec IN (SELECT deptno, dname,loc FROM dept) LOOP
-- Print the foo and bar values
dbms_output.put_line('deptno=' || v_rec.deptno || ', dname=' || v_rec.dname||',loc='||v_rec.loc);
END LOOP;
end;
begin
    pro_insertDept(31,'sal','xm');
       -- Open the cursor and loop through the records
       FOR v_rec IN (SELECT deptno, dname,loc FROM dept) LOOP       
       -- Print the foo and bar values
       dbms_output.put_line('deptno=' || v_rec.deptno || ', dname=' || v_rec.dname||',loc='||v_rec.loc);
       END LOOP;
end;
begin
    pro_insertDept(31,var_ename=>'mainten','xm');
       -- Open the cursor and loop through the records
       FOR v_rec IN (SELECT deptno, dname,loc FROM dept) LOOP       
       -- Print the foo and bar values
       dbms_output.put_line('deptno=' || v_rec.deptno || ', dname=' || v_rec.dname||',loc='||v_rec.loc);
       END LOOP;
end;
 

1.note

         in is input parameter type.

1.specify name passing parameter with in model  is disorder.

advantage:understand meaning when you look at the codeing

disadvantage:if insert data a large number and you have a lot of work

2.follow the location passing parameter is order

a.if you forget parameter order can use desc keword.

3.meger passing patameter.

a.if you use specify name passing parameter pass values.after coding use the same method,also.because specify name passing parameter has a order.

b.out model

create or replace procedure select_dept(
num_deptno in number,--定义in模式变量,要求输入部门编号
var_dname out dept.dname%type,--定义out模式变量,可以存储部门名称并输出
var_loc out dept.loc%type) is
begin
select dname,loc
into var_dname,var_loc
from dept
where deptno = num_deptno;--检索某个部门编号的部门信息
exception
when no_data_found then --若select语句无返回记录
dbms_output.put_line('该部门编号的不存在');--输出信息
end select_dept;
/
DECLARE
var_dname dept.dname%type;
var_loc dept.loc%type;
BEGIN
select_dept(,var_dname,var_loc);
DBMS_OUTPUT.PUT_LINE('dept name is:'||var_dname||',location is:'||var_loc);
END;

variable var_dname varchar2(50);
variable var_loc varchar2(50);
execute select_dept(15,:var_dname,:var_loc);
if you not display result then can use below sentences
  1.print var_dname var_loc;
  2.select :var_danme,:var_loc from dua;

summary:

  out is a output paramter type.

  step:

    1.create procedure.

    2.define declare variable type in pl/sql block,in order to incept out values.

    3.call stored procedure incept return value in out parameter.

  note:you need define variable in out model otherwise show error give you in the progarm.

c.in out model

in out model is input parameter as well as output parameter.

oracle_procedure的更多相关文章

随机推荐

  1. 洛谷 P4108 / loj 2119 [HEOI2015] 公约数数列 题解【分块】

    看样子分块题应该做的还不够. 题目描述 设计一个数据结构. 给定一个正整数数列 \(a_0, a_1, \ldots , a_{n-1}\),你需要支持以下两种操作: MODIFY id x: 将 \ ...

  2. 高精度的N进制转换模板(转K神)

    /* 高精度进制转换 把oldBase 进制的数转化为newBase 进制的数输出. 调用方法,输入str, oldBase newBase. change(); solve(); output(); ...

  3. vue 同一个组件的跳转, 返回时保留原来的下拉位置

    1,需求分析 公司的项目有这样一个需求: 同一个list组件,根据传过来的listId渲染成多个页面,每个页面都可以下拉.在返回到不同的list页面时,要保留当时下拉的位置. 说的我自己都有点懵逼了, ...

  4. 1152 Google Recruitment (20 分)

    In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...

  5. [转] Java8 日期/时间(Date Time)API指南

    [From] http://www.importnew.com/14140.html Java 8日期/时间( Date/Time)API是开发人员最受追捧的变化之一,Java从一开始就没有对日期时间 ...

  6. Q443 压缩字符串

    给定一组字符,使用原地算法将其压缩. 压缩后的长度必须始终小于或等于原数组长度. 数组的每个元素应该是长度为1 的字符(不是 int 整数类型). 在完成原地修改输入数组后,返回数组的新长度. 进阶: ...

  7. PIE SDK介绍

    1. 产品概述 PIE-SDK是航天宏图自主研发的PIE二次开发组件包,集成了专业的遥感影像处理.辅助解译.信息提取.专题图表生成.二三维可视化等功能.底层采用微内核式架构,由跨平台的标准C++编写, ...

  8. 转 AIX7.2+11.2.0.4RAC实施

    参考 https://blog.csdn.net/alangmei/article/details/18310381 https://blog.csdn.net/smasegain/article/d ...

  9. golang中的文件操作

    一.文件的基本介绍 文件是数据源(保存数据的地方)的一种,比如经常使用的word文档,txt文件,excel文件都是文件.文件最主要的作用就是保存数据,它既可以保存一张图片,也可以保持视频,声音等等. ...

  10. flask综合整理1

    前言: 框架的对比 Django:1个重武器,包含了web开发中常用的功能.组件的框架:(ORM.Session.Form.Admin.分页.中间件.信号.缓存.ContenType....): To ...