優(yōu)點比較
sys_refcursor,可以在存儲過程中作為參數(shù)返回一個table格式的結(jié)構(gòu)集(我把他認(rèn)為是table類型,容易理解,其實是一個游標(biāo)集), cursor 只能用在存儲過程,函數(shù),包等的實現(xiàn)體中,不能做參數(shù)使用。
sys_refcursor 這東西可以使用在包中做參數(shù),進(jìn)行數(shù)據(jù)庫面向?qū)ο箝_放。哈哈。我喜歡。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;
補(bǔ)充:Oracle存儲過程返回select * from table結(jié)果
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.在程序中調(diào)用存儲過程的值
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。并且輸入?yún)?shù) 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);
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
標(biāo)簽:泰安 柳州 南陽 銀川 酒泉 河源 梧州 焦作
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《oracle 存儲過程返回 結(jié)果集 table形式的案例》,本文關(guān)鍵詞 oracle,存儲,過程,返回,結(jié)果,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。