本篇文章为大家展示了怎么在oracle存储过程返回结果集,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
优点比较
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);
上述内容就是怎么在oracle存储过程返回结果集,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。