oracle_procedure
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的更多相关文章
随机推荐
- UVA - 11404
题意:求任意删除字符后所形成的最长回文,并输出字典序最小的方案 把原串反转求LIS,因为转移时不断求字典序最小导致后半部分可能并非回文,所以要前半部分输出两边 话说这方案保存可真暴力 #include ...
- 2019.3.13 Java的特性——继承
继承 面向对象编程(OOP)三大特征:继承,封装,多态 目的:为了减少重复代码,避免复制粘贴 创建父类Animal public class Animal { private String name; ...
- python 可迭代对象,迭代器和生成器,lambda表达式
分页查找 #5.随意写一个20行以上的文件(divmod) # 运行程序,先将内容读到内存中,用列表存储. # l = [] # 提示:一共有多少页 # 接收用户输入页码,每页5条,仅输出当页的内容 ...
- Flutter 导入 import 'package:english_words/english_words.dart'
import 'package:flutter/material.dart';import 'package:english_words/english_words.dart'; // 导入的包 vo ...
- Navicat Premium Mac 12 破解(CV别人的,但是亲测能用)
第一步:这部分暂时存到文本编辑器中 公钥: -----BEGIN PUBLIC KEY-----MIIBITANBgkqhkiG9w0BAQEFAAOCAQ4AMIIBCQKCAQB8vXG0ImYh ...
- oracle 基础知识(四) 构成
一, oracle服务 一个oracle 服务由一个oracle 实例和一个oracle数据库组成. oracle = instance + database 总体概念: 二, oracle 实例 0 ...
- 【Tensorflow】 Object_detection之liunx服务器安装部署步骤记录
环境:centos7+anaconda python3.6 步骤: 1.下载Models cd 到预存放目录下,执行: git clone https://github.com/tensorflow/ ...
- intellij idea开发过程中遇到的问题
https://blog.csdn.net/wonder_dog/article/details/79289883
- hibernate 简单入门 配置
hibernate两个配置文件,一个是类和表的映射关系文件,一个是数据库连接的配置文件 类和表的映射关系 <?xml version="1.0" encoding=" ...
- 揭秘TDSQL全时态数据库系统的核心技术
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯技术工程官方号发表在腾讯云+社区 Design 本节讨论T-TDSQL的关键之处,即影响T-TDSQL架构的设计之处.一是新的数据 ...