Oracle 存储过程返回结果集 (转)[@more@]
1.返回数组 (作者:/Message_Board/Send.?sendto=enhydraboy" target=_blank>enhydraboy(乱舞的浮尘))
在后台创建一个包或者过程
connect tt/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 employee_id,first_name||','||Middle_Initial||','||last_name as name from employee;
cnt NUMBER DEFAULT 1;
c c1%ROWTYPE;
BEGIN
open c1;
L
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 ADO.Connection
Dim rs As New ADODB.Recordset
Dim cmd As New mand
Dim str As String
str = "{call ado_callpkg.getEmpNames({resultset 100,empid,empname})}"
cn.Open "Provr=MSDAORA.1;Pass=tiger;User ID=scott;Data =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
De.Print rs.Fields(0).Value & vbTab & rs.Fields(1).Value
rs.MoveNext
Loop
------------
总结
1 oracle的后台存储过程,应该通过一个类似数组并且带有数字的变量返回,有多少个列,就有对应多少个变量
2 前台,调用的语句写法要注意,
{call .(,,....,{resultset ,,,...})}
注意的细节,
(1) 要自己指定一个数字,表示接受的行数大小,如果太小,而实际返回的记录大于这个数字,会出错
(2) 如果有输入参数,应该在command中创建输入参数,对应的地方用?替代,如
{call ado_callpkg.getEmpNames(?,{resultset 100,empid,empname})}
(3) output和你存储的定义一致,参数名要一样,次序也一样,否则也会出错。