Oracle 存儲過程返回結果集
2024-08-29 13:31:10
供稿:網友
,歡迎訪問網頁設計愛好者web開發。
1.返回數組 (作者:enhydraboy(亂舞的浮塵) )
在oracle后臺創建一個程序包或者存儲過程
connect scott/tiger;
create or replace package ado_callpkg as
type eid is table of number(4) index by binary_integer;
type ename is table of varchar2(40) index by binary_integer;
procedure getempnames (empid out eid,empname out ename);
end ado_callpkg;
create or replace package body ado_callpkg as
procedure getempnames (empid out eid,empname out ename) is
cursor c1 is select employee_id,first_name||','||middle_initial||','||last_name as name from employee;
cnt number default 1;
c c1%rowtype;
begin
open c1;
loop
fetch c1 into c;
empname(cnt):=c.name;
empid(cnt):=c.employee_id;
exit when c1%notfound; -- process the data
cnt :=cnt+1;
end loop;
close c1;
end;
end ado_callpkg;
2 前臺vb程序調用
dim cn as new adodb.connection
dim rs as new adodb.recordset
dim cmd as new adodb.command
dim str as string
str = "{call ado_callpkg.getempnames({resultset 100,empid,empname})}"
cn.open "provider=msdaora.1;password=tiger;user id=scott;data source=oracle;persist security info=true"
with cmd
.commandtext = str
.activeconnection = cn
.commandtype = adcmdtext
end with
rs.cursorlocation = aduseclient
rs.open cmd
do while not rs.eof
debug.print rs.fields(0).value & vbtab & rs.fields(1).value
rs.movenext
loop
------------
總結
1 oracle的后臺存儲過程,應該通過一個類似數組并且帶有數字索引的變量返回,有多少個列,就有對應多少個變量
2 前臺,調用的sql語句寫法要注意,
{call <package_name>.<prodecure name>(<input1>,<input2>,....<inputn>,{resultset <number>,<output1>,<output2>,...<outputn>})}
注意的細節,
(1) <number>要自己指定一個數字,表示接受的行數大小,如果太小,而實際返回的記錄大于這個數字,會出錯
(2) 如果有輸入參數,應該在command中創建輸入參數,對應的地方用?替代,如
{call ado_callpkg.getempnames(?,{resultset 100,empid,empname})}
(3) output和你存儲函數的定義一致,參數名要一樣,次序也一樣,否則也會出錯。