oracle使用游標讓儲存過程返回記錄集

2021-09-30 10:14:11 字數 1630 閱讀 6992

因為專案需要,需要寫乙個儲存過程在oracle上,返回的是記錄集,普通的儲存過程貌似不可以,按照網上說的用游標寫了乙個出來,如下:

--宣告包頭

create or replace package pack_defectscount as

type cur_defectscount is ref cursor;

procedure up_defectscount(startdate in date,

enddate in date,

p_cur out cur_defectscount);

end pack_defectscount;

--宣告包體

create or replace package body pack_defectscount as

procedure up_defectscount(startdate in date, enddate in date, p_cur out cur_defectscount) is

sqlstring varchar2(1000);

begin

sqlstring := 'select

'''' abbreviation,

'''' findcount,

'''' completecount,

'''' overtimecount,

'''' completerate,

'''' findcount1,

'''' completecount1,

'''' overtimecount1,

'''' completerate1,

'''' findcount2,

'''' completecount2,

'''' overtimecount2,

'''' completerate2,

'''' findcount3,

'''' completecount3,

'''' overtimecount3,

'''' completerate3

from dual

where :startdate = :startdate

and :enddate = :enddate';

open p_cur for sqlstring using startdate,startdate,enddate,enddate;

end up_defectscount;

end pack_defectscount;

然後大概的解釋下,在包頭檔案中定義的cursor就是游標型別,然後是儲存過程的輸入輸出引數,下面就是包體,執行時要包頭包體分開執行,下面只是我隨意寫的乙個sql只是為了有欄位而已(是from dual表的,賦的空值),引數那邊也是為了試試引數是否傳遞過來構造的乙個類似1=1的效果,open sql的時候因為我這樣寫的所以引數各被呼叫二次,實際上應該是一次就可以的,還有就是如果你的sql短的話可以不定義變數再open,直接放到open 'sql' using param也一樣的,其他沒什麼要說的了,照葫蘆畫瓢就ok。測試的時候在plsql下對應的package bodies,然後右鍵view你的package然後選到procedure右鍵test ,填上輸入引數,執行後下面的游標那行後面有個[...]的按鈕,點開就能看到啦~

Oracle使用游標迴圈呼叫儲存過程

宣告游標 cursor cursor name is select statement for 迴圈游標 1 定義游標 2 定義游標變數 3 使用for迴圈來使用這個游標 declare 型別定義 cursor c job is select a.workorderid from idc.pf or...

Mysql 儲存過程使用游標

完整例子 create procedure test begin 定義引數 declare id int 定義游標 declare no more products int default 0 declare result test cursor for select num1 from numte...

oracle儲存過程呼叫游標例子

1 首先你需要建立乙個包,並定義你返回的游標的型別 儲存過程 create or replace package test pkg is 定義游標 type t cur is ref cursor 儲存過程宣告 procedure test proc p cur in out t cur end t...