之前项目写了一段时间存储过程,抽点时间做个简单的总结,与大家共享。
数据库使用:
--Oracle存储过程创建语法:
--创建存储过程 create or replace procedure 存储过程名(param1 in type,param2 out type)
create or replace procedure test_proc(v_type in varchar2)
as
--常用变量,格式-变量 类型(值范围);
v_tsql varchar2(2000);
v_test_name varchar2(50);
v_id varchar2(50);
v_rule varchar2(50);
--游标声明及赋值 cursor 游标名(参数名 参数类型) is sql查询语句,条件可以包含参数,注:表名不可用参数代替,识别不出来。
CURSOR get_test_info(v_test_name varchar2) is
select t.id,t.rule from testA t where t.status = 1 and t.test_name = v_test_name;
--开始存储过程
Begin
--执行游标,获取符合sql条件的数据列for 对象 in 游标名 loop
for job_row in get_test_info(v_type) loop
--开始循环
begin
--判断对象中属性rule(为游标sql查询列)不为空
if job_row.rule is not null then
--删除sql赋值
v_tsql:='delete from testA where id ='''||job_row.id||''' and trunc(d_day) = trunc(sysdate)';
--执行
execute immediate v_tsql;
--提交
commit;
--插入sql赋值,出现变量值:1,:2,为插入变量值,类似java执行sql语句中'?'
v_tsql:= 'insert into testA (name) select name from testB where id = :1 ';
--执行,using 变量值,顺序对应v_tsql中的:1,:2
execute immediate v_tsql using job_row.id;
commit;
--输出sql,可以查看内容
dbms_output.put_line(v_tsql);
end if;
EXCEPTION
When others then
begin
dbms_output.put_line(SQLERRM);
end ;
end;
end loop;
下面为存储过程其他循环方式,与上面for...loop等同
--游标循环 while写法
--打开游标
open get_test_info(v_type);
fetch get_test_info into v_id,v_rule;
while get_test_info%FOUND loop
if job_row.rule is not null then
--删除sql赋值
v_tsql:='delete from testA where id ='''||job_row.id||''' and trunc(d_day) = trunc(sysdate)';
--执行
execute immediate v_tsql;
--提交
commit;
--插入sql赋值,出现变量值:1,:2,为插入变量值,类似java执行sql语句中'?'
v_tsql:= 'insert into testA (name) select name from testB where id = :1 ';
--执行,using 变量值,顺序对应v_tsql中的:1,:2
execute immediate v_tsql using job_row.id;
commit;
--输出sql,可以查看内容
dbms_output.put_line(v_tsql);
end if;
fetch get_test_info into v_id,v_rule;
end loop;
close get_test_info;
--游标循环 Loop写法
--打开游标
open get_test_info(v_type);
loop
fetch get_test_info into v_id,v_rule;
EXIT WHEN get_test_info%NOTFOUND;
if v_id is not null then
--删除sql赋值
v_tsql:='delete from testA where id ='''||v_id||''' and trunc(d_day) = trunc(sysdate)';
--执行
execute immediate v_tsql;
--提交
commit;
--插入sql赋值,出现变量值:1,:2,为插入变量值,类似java执行sql语句中'?'
v_tsql:= 'insert into testA (name) select name from testB where id = :1 ';
--执行,using 变量值,顺序对应v_tsql中的:1,:2
execute immediate v_tsql using v_id;
commit;
exit;
--输出sql,可以查看内容
dbms_output.put_line(v_tsql);
end if;
end loop;
close get_test_info;
java调用:
String procedure = "{call test_proc(?) }";
//取得数据库连接
CallableStatement call = aaa.getConnection().prepareCall(procedure);
call.setString(1, opertype);
// 执行
call.execute();
分享到:
相关推荐
oracle存储过程使用文档中描述了存储过程创建的过程,和一些注意事项
oracle存储过程使用游标对多表操作例子
NULL 博文链接:https://snowelf.iteye.com/blog/575555
超详细Oracle存储过程使用手册 超详细Oracle存储过程使用手册
oracle存储过程使用详解,初学者可以很快入门
Oracle存储过程中使用临时表 会话级临时表 事务级临时表
oracle存储过程使用游标判断2表差异,进行新增更新操作
利用游标返回结果集的的例子(Oracle 存储过程).doc 利用游标返回结果集的的例子(Oracle 存储过程).doc 利用游标返回结果集的的例子(Oracle 存储过程).doc
oracle 存储过程导出excel oracle 存储过程导出excel oracle 存储过程导出excel oracle 存储过程导出excel oracle 存储过程导出excel
本文实例讲述了Python使用cx_Oracle调用Oracle存储过程的方法。分享给大家供大家参考,具体如下: 这里主要测试在Python中通过cx_Oracle调用PL/SQL。 首先,在数据库端创建简单的存储过程。 create or replace ...
oracle 存储过程实例 oracle存储过程实例
【精华志】超详细Oracle存储过程使用手册 QQ交流群60168829 欢迎C# ASP.NET 和SQL新手 老手 高手加入 谢谢
使用java实现oracle存储过程。 共有3个小例子。实现的功能 1、无返回值的存储过程 如 insert 2、有返回值的存储过程(非列表)select id from tab 3、返回列表 如:select * from tab 顺便鄙视下csdn,作为一个it...
ibatis调用oracle存储过程分页
oracle 存储过程 unwrap 图形解密 工具
oracle存储过程笔记oracle存储过程笔记oracle存储过程笔记oracle存储过程笔记oracle存储过程笔记oracle存储过程笔记oracle存储过程笔记oracle存储过程笔记oracle存储过程笔记oracle存储过程笔记
帆软报表调用Oracle存储过程如果存储过程定义中没有参数,但是设计器中缺弹出一个storeParameter1参数的解决插件
可以将SQL Server存储过程转为oracle存储过程的工具
oracle 存储过程 函数 dblink 绝对对工作和平时学习有价值的资料。针对个人具体情况做修改即可使用
oracle 存储过程批量提交