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 - 11584 DP 最少线段覆盖
题意:用最少的不可交线段覆盖整个区间,求该最小值 课上摸鱼的时候没注意到题意的转换,写了没啥卵用的回文中心最长枚举,所以代码里的st和h/h2是几乎没用的 注意状态转移的时候不要只用最长线段去转移,这 ...
- 选择IM云服务供应商
选择IM云服务供应商,其实最重要是有三个因素:费用.技术稳定性.以及后续运维服务. 对于不少创业公司来讲,可能需要找到成本和稳定性的最佳平衡点.目前国内不少IM云服务产品都推出了免费服务项目或者一定期 ...
- [转] 配置文件解析利器-Config库
[From] https://blog.csdn.net/zero__007/article/details/51493851 Typesafe的Config库,纯Java写成.零外部依赖.代码精简. ...
- 经常谈到Oracle的权限,你究竟知道多少
作者: 三十而立 时间:2009年10月28日 9:41:15 请尊重原创作品.转载请保持文章完整性,并以超链接形式注明原始作者"inthirties(三十而立)" 接着上面谈到的 ...
- Hash索引和B+树索引总结
先说Hash索引 在理想的情况下,key非常分散,不存在Hash碰撞的话,采用Hash索引可以唯一得确定一个key的位置,并且这个位置上就只有一个key,所以查找时间复杂度是O(1),非常快,这是Ha ...
- Linux VFS机制简析(二)
Linux VFS机制简析(二) 接上一篇Linux VFS机制简析(一),本篇继续介绍有关Address space和address operations.file和file operations. ...
- java使用netty的模型总结
一 由于本人的码云太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的github项目地址 https://github.com/247292980/spring-boo ...
- Git使用教程,感觉比较全,所以【转载】
一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...
- 线程操作之Thread类
在.Net fremework 中,所有与线程有关的操作都封装在System.Thread命名空间中, 所以在操作线程时,要先引入次命名空间 Thread类的常用方法 Abort 线程终止 Join ...
- Java ConcurrentHashMap初始化
初始化ConcurrentHashMap时可以指定map大小,由于ConcurrentHashMap代码默认大小是2n,这里需要把用户填的大小转换成2n备注:代码基于jdk 1.8.0_91Concu ...