–sys_refcursor 和 cursor 优缺点比较
优点比较
优点一:
sys_refcursor,可以在存储过程中作为参数返回一个table格式的结构集(我把他认为是table类型,容易理解,其实是一个游标集), cursor 只能用在存储过程,函数,包等的实现体中,不能做参数使用。
优点二:
sys_refcursor 这东西可以使用在包中做参数,进行数据库面向对象开放。哈哈。我喜欢。cursor就不能。
create or replace procedure p_test(p_cur out sys_refcursor)
as
begin
open p_cur for select * from emp;
end p_test;
declare
p_cur sys_refcursor;
i emp%rowtype;
begin
p_test(p_cur);
loop fetch p_cur
into i;
exit when p_cur%notfound;
dbms_output.put_line('---'||i.ename||'---'||i.empno);
end loop;
close p_cur;
end;
补充:oracle存储过程返回select * from table结果
1.首先建立一个包
create or replace package logoperation is type listlog is ref cursor; procedure pcenterexamine_sel(listcenterexamine out listlog,testlist out listlog,numpage in decimal); end;
2.建立包中的主体
create or replace package body logoperation is procedure pcenterexamine_sel ( listcenterexamine out listlog, testlist out listlog, numpage in decimal ) as begin open listcenterexamine for select * from log_centerexamine; open testlist for select * from log_centerexamine; end; end;
3.在程序中调用存储过程的值
public static dataset runproceduregetdataset(string storedprocname, oracleparameter[] parameters)
{
string connectionstring ="192.168.1.1/db";
using (oracleconnection connection = new oracleconnection(connectionstring))
{
dataset dataset = new dataset();
connection.open();
oracledataadapter sqlda = new oracledataadapter();
sqlda.selectcommand = buildquerycommand(connection, storedprocname, parameters);
sqlda.fill(dataset, "dt");
connection.close();
return dataset;
}
}
private static oraclecommand buildquerycommand(oracleconnection connection, string storedprocname, idataparameter[] parameters)
{
oraclecommand command = new oraclecommand(storedprocname, connection);
command.commandtype = commandtype.storedprocedure;
foreach (oracleparameter parameter in parameters)
{
command.parameters.add(parameter);
}
return command;
}
4.有几个out的ref cursor,变量ds中就用几个datatable。并且输入参数 indecimal也不会受影响,并且可以参加存储过程的运算
oracleparameter[] paramdic = {
new oracleparameter("listcenterexamine",oracletype.cursor),
new oracleparameter("testlist",oracletype.cursor),
new oracleparameter("numpage",oracletype.int32)};
paramdic[0].direction = parameterdirection.output;
paramdic[1].direction = parameterdirection.output;
paramdic[2].value = 1;
ds = model.oraclehelper.runproceduregetdataset("logoperation.pcenterexamine_sel", paramdic);
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。如有错误或未考虑完全的地方,望不吝赐教。