VBA调用带游标返回值的ORACLE存储过程
2011-08-30 00:19
375 查看
【解决方案一】:
On Error GoTo 11
Dim hisCNN As ADODB.Connection
Dim hisRST As ADODB.Recordset
Dim sSQL As String
Dim iZDS As Integer
Dim sBT As String
Dim sTEMP As String
Dim hisCMD As New ADODB.Command
Dim Para1 As New ADODB.Parameter
Dim Para2 As New ADODB.Parameter
Dim Para3 As New ADODB.Parameter
Set hisCNN = New ADODB.Connection
hisCNN.CursorLocation = adUseClient
hisCNN.Open "Provider=MSDASQL.1;Persist Security Info=False;User ID=i_pathology;pwd=i_pathology;Data Source=aaa"
MsgBox "连接数据库成功!"
//aaa是ODBC名
Set hisCMD.ActiveConnection = hisCNN
hisCMD.CommandType = adCmdStoredProc
hisCMD.CommandText = "zhi.F_get_sick_info"
Set Para1 = hisCMD.CreateParameter("as_patient_id", adVarChar, adParamInput, 100, "00")
Para1.Value = "3333333"
hisCMD.Parameters.Append Para1
Set Para2 = hisCMD.CreateParameter("as_io_flag", adVarChar, adParamInput, 100, "1")
Para2.Value = "1"
hisCMD.Parameters.Append Para2
MsgBox "hiscmd"
Set hisRST = hisCMD.Execute
MsgBox "得到记录集!"
如果把连接改成
Provider=msdaora.1;Data Source=服务名;User ID=i_pathology;Password=i_pathology;
的话,提示 参数数量和类型不对.第三个参数是 游标,我查了,应该不用赋值的.
【解决方案二】:
PL/SQL 代码:
CREATE OR REPLACE PACKAGE "SCOTT"."PKG_TEST" AS
TYPE myrcType IS REF CURSOR;
FUNCTION get(strbarcode VARCHAR) RETURN myrcType;
END pkg_test;
CREATE OR REPLACE PACKAGE BODY "SCOTT"."PKG_TEST" AS
FUNCTION get(strbarcode IN VARCHAR) RETURN myrcType IS
rc myrcType;
BEGIN
OPEN rc FOR strbarcode;
RETURN rc;
END get;
END pkg_test;
--------------------------------------------------------------------------------------------------------
VB 代码:
Private Sub Command1_Click()
On Error GoTo cursorErr:
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim cmd As New ADODB.Command
cnn.ConnectionString = "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott;Data Source=oraAny;Extended Properties=PLSQLRSet=1"
cnn.Open
With cmd
.ActiveConnection = cnn
.CommandType = adCmdText
.CommandText = "{CALL scott.pkg_test.get(?)}"
.Parameters.Append .CreateParameter("strBarCode", adVarChar, adParamInput, 100, "SELECT * FROM TAB")
End With
rst.CursorType = adOpenStatic
rst.LockType = adLockReadOnly
Set rst.Source = cmd
rst.Open
MsgBox rst.RecordCount
Set rst = Nothing
Set cmd = Nothing
Exit Sub
cursorErr:
Set cmd = Nothing
Set rst1 = Nothing
MsgBox Err.Description
End Sub
On Error GoTo 11
Dim hisCNN As ADODB.Connection
Dim hisRST As ADODB.Recordset
Dim sSQL As String
Dim iZDS As Integer
Dim sBT As String
Dim sTEMP As String
Dim hisCMD As New ADODB.Command
Dim Para1 As New ADODB.Parameter
Dim Para2 As New ADODB.Parameter
Dim Para3 As New ADODB.Parameter
Set hisCNN = New ADODB.Connection
hisCNN.CursorLocation = adUseClient
hisCNN.Open "Provider=MSDASQL.1;Persist Security Info=False;User ID=i_pathology;pwd=i_pathology;Data Source=aaa"
MsgBox "连接数据库成功!"
//aaa是ODBC名
Set hisCMD.ActiveConnection = hisCNN
hisCMD.CommandType = adCmdStoredProc
hisCMD.CommandText = "zhi.F_get_sick_info"
Set Para1 = hisCMD.CreateParameter("as_patient_id", adVarChar, adParamInput, 100, "00")
Para1.Value = "3333333"
hisCMD.Parameters.Append Para1
Set Para2 = hisCMD.CreateParameter("as_io_flag", adVarChar, adParamInput, 100, "1")
Para2.Value = "1"
hisCMD.Parameters.Append Para2
MsgBox "hiscmd"
Set hisRST = hisCMD.Execute
MsgBox "得到记录集!"
如果把连接改成
Provider=msdaora.1;Data Source=服务名;User ID=i_pathology;Password=i_pathology;
的话,提示 参数数量和类型不对.第三个参数是 游标,我查了,应该不用赋值的.
【解决方案二】:
PL/SQL 代码:
CREATE OR REPLACE PACKAGE "SCOTT"."PKG_TEST" AS
TYPE myrcType IS REF CURSOR;
FUNCTION get(strbarcode VARCHAR) RETURN myrcType;
END pkg_test;
CREATE OR REPLACE PACKAGE BODY "SCOTT"."PKG_TEST" AS
FUNCTION get(strbarcode IN VARCHAR) RETURN myrcType IS
rc myrcType;
BEGIN
OPEN rc FOR strbarcode;
RETURN rc;
END get;
END pkg_test;
--------------------------------------------------------------------------------------------------------
VB 代码:
Private Sub Command1_Click()
On Error GoTo cursorErr:
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim cmd As New ADODB.Command
cnn.ConnectionString = "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott;Data Source=oraAny;Extended Properties=PLSQLRSet=1"
cnn.Open
With cmd
.ActiveConnection = cnn
.CommandType = adCmdText
.CommandText = "{CALL scott.pkg_test.get(?)}"
.Parameters.Append .CreateParameter("strBarCode", adVarChar, adParamInput, 100, "SELECT * FROM TAB")
End With
rst.CursorType = adOpenStatic
rst.LockType = adLockReadOnly
Set rst.Source = cmd
rst.Open
MsgBox rst.RecordCount
Set rst = Nothing
Set cmd = Nothing
Exit Sub
cursorErr:
Set cmd = Nothing
Set rst1 = Nothing
MsgBox Err.Description
End Sub
相关文章推荐
- .net调用oracle存储过程返回值时遇到的问题
- java 调用Oracle存储过程(输入参数,输出参数,游标)的使用!
- java调用 Oracle存储过程(或函数) 返回游标、动态数组与
- Java调用Oracle存储过程,以及Oracle中游标的使用
- .net调用带游标输出的oracle存储过程
- mybatis调用oracle存储过程返回游标 读取到java的List<Map>
- ASP.NET调用oracle存储过程返回多个游标
- java调用带有返回值的oracle存储过程
- excel中vba调用java程序并获取返回值
- Java调用Oracle存储过程(无返回值)
- java jdbc 调用 oracle存储过程 返回游标
- ibatis调用oracle存储过程,返回游标集合
- asp.net调用Oracle存储过程,获取返回值!(例子)
- oracle存储过程调用游标例子
- oracle存储过程,package,传参数值。游标返回值。
- mybatis 调用oracle存储过程,传参、返回游标的值获取--示例
- SSM-Mybatis调用Oracle存储过程返回结果集(游标)
- c#调用Oracle存储过程返回的游标结果代码实例
- JQuery获取表单绑定的submit事件并手动调用获得返回值
- C语言调用存储过程并且获得返回值