oracle存储过程

1,存储过程实例一,参数的in可以省略。参数默认为输入参数。

create or replace procedure p1(pname in varchar2,newSal in number) is

begin

update emp set sal = newSal where name = pname;

end;


2,java程序中调用上述存储过程的方法:

//先获得数据库连接connection,

connection conn = ...

CallableStatement cs = conn.prepareCall("{call p1(?,?)}");

cs.setString(1,"zhangsan");

cs.setInt(2,5000);


cs.execute();

cs.close();

conn.close();


3,定义pl/sql记录的实例

declare

type emp_record_type is record(name emp.name%type,sal emp.sal%type,title emp.title%type);

sp_record emp_record_type; --定义 emp_record_type类型的变量sp_record

begin

select name,sal,title into sp_record

from emp where empno = '1111';

dbms_output.put_line("name:"|| sp_record.name);

end;

/


4,定义和使用表类型(相当于java中的数组)

declare 

type sp_table_type is table of emp.name%type index by binary_integer;

sp_table sp_table_type;

begin

select name into sp_table(0) from emp where empno = '1111';

dbms_output.put_line("name:" || sp_table(0));

end;


5,定义和使用游标类型

declare

type sp_emp_cursor is ref cursor; --定义游标类型

test_cursor sp_emp_cursor;

v_name emp.name%type;

begin

open test_cursor for select name,sal from emp;

loop

fetch test_cursor into v_name;

exit when test_cursor%notfound;

dbms_output.put_line("name:"|| v_name);

end loop

end;


6,存储过程实例二,返回结果集的过程

create or replace package testpackage as

type test_cursor is ref cursor;

end testpackage;


create or replace procedure p1(pname in varchar2,p_cursor out testpackage.test_cursor) is

begin

open p_cursor for select * from emp where name like '%'||panme||'%';

end;




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值