--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);
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- oracle通過存儲過程上傳list保存功能
- 解決PL/SQL修改Oracle存儲過程編譯就卡死的問題
- 關于Oracle存儲過程和調度器實現自動對數據庫過期數據清除的問題
- Oracle如何批量將表中字段名全轉換為大寫(利用簡單存儲過程)
- Spring boot調用Oracle存儲過程的兩種方式及完整代碼
- oracle調試存儲過程的過程詳解
- Oracle如何使用PL/SQL調試存儲過程
- Oracle存儲過程案例詳解