ITPub博客

首页 > 数据库 > Oracle > VBA宏调用Oracle PL/SQL Procedure

VBA宏调用Oracle PL/SQL Procedure

原创 Oracle 作者:pan_tian 时间:2014-07-02 14:47:15 0 删除 编辑

1.定义一个测试Procedure,这个Procedure用于给临时表插入一条记录

create  sequence pt_debug_sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
CACHE 10;

--1. Create temp table.
create  table pt_debug_tab (seq INTEGER,text Varchar2(300),datetag Varchar2(30));

--2. Create procedure.
create or replace
procedure pt_debug(inStr Varchar2) as
PRAGMA AUTONOMOUS_TRANSACTION; 
BEGIN 
INSERT INTO pt_debug_tab VALUES(pt_debug_sequence.NEXTVAL,inStr,to_char(sysdate,\'hh:mi:ss\')); 
COMMIT; 
END;

2.VBA中的代码,参照:VBA - Hello World 样式,在Sheet中插入一个测试按钮,然后在按钮事件中加入下边的Macro代码

Private Sub CommandButton1_Click()
 
Dim rs As Object, com As Object, cn As Object

Set cn = CreateObject("ADODB.Connection")
cn.Open ("Provider=MSDAORA.1;Password=password;Persist Security Info=True;User ID=user;Data Source=instance_name")
\'Update the connectionstring
\'Set com = CreateObject("ADODB.Command")
Set rs = CreateObject("ADODB.Recordset")
Set com = CreateObject("ADODB.Command")
Set com.ActiveConnection = cn
com.CommandText = "BEGIN pt_debug(\'PTIAN:Sysdate is \'||sysdate); END;"
com.CommandType = 1

Set rs = com.Execute

MsgBox ("Done!")
 
End Sub

执行后,能够看到pt_debug_tab表插入了一条新的记录。


<!-- Baidu Button BEGIN -->

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26687597/viewspace-1204543/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: VBA Code Samples
下一篇: Protect/Lock Excel VBA Code
请登录后发表评论 登录
全部评论
暂无介绍

注册时间:2012-02-18

最新文章