各位用户为了找寻关于oracle 存储过程返回 结果集 table形式的案例的资料费劲了很多周折。这里教程网为您整理了关于oracle 存储过程返回 结果集 table形式的案例的相关资料,仅供查阅,以下为您介绍关于oracle 存储过程返回 结果集 table形式的案例的详细内容
--sys_refcursor 和 cursor 优缺点比较
优点比较
优点一:
sys_refcursor,可以在存储过程中作为参数返回一个table格式的结构集(我把他认为是table类型,容易理解,其实是一个游标集), cursor 只能用在存储过程,函数,包等的实现体中,不能做参数使用。
优点二:
sys_refcursor 这东西可以使用在包中做参数,进行数据库面向对象开放。哈哈。我喜欢。cursor就不能。
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17create
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.首先建立一个包
? 1 2 3 4create
or
replace
package LogOperation
is
type listLog
is
ref
cursor
;
procedure
PCenterExamine_sel(listCenterExamine
out
listlog,testlist
out
listLog,numpage
in
decimal
);
end
;
2.建立包中的主体
? 1 2 3 4 5 6 7 8 9 10 11 12 13create
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.在程序中调用存储过程的值
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14public
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;
}
}
?
1
2
3
4
5
6
7
8
9
10
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也不会受影响,并且可以参加存储过程的运算
? 1 2 3 4 5 6 7 8OracleParameter[] 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);
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/ruiguang21/article/details/80418909