Oracle 存储过程返回结果集

发表于:2007-07-02来源:作者:点击数: 标签:
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) INDE

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

 

原文转自:http://www.ltesting.net