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和你存储函数的定义一致,参数名要一样,次序也一样,否则也会出错。