首页 > 解决方案 > 从 MS Excel VBA 宏启动 .bat 文件(通过 SQLcl 连接到 Oracle)

问题描述

我正在编写一个需要:

  1. 启动 .bat 文件
  2. 等到 .bat 文件完成(数据通​​过 SQLcl 从 Oracle 下载到 .csv)
  3. 继续执行 VBA 的其余部分

我为这个问题找到了两种解决方案:

第一个 - 通过壳牌:

Dim shell_command As String
shell_command = ".bat_file_location"
Call Shell(shell_command, vbNormalFocus)

这会打开 .bat 文件,但窗口会立即关闭并且不执行任何操作。

第二个 - 通过 Windows 脚本宿主对象:

Dim strCommand As String
Dim lngErrorCode As Long
Dim wsh As WshShell
Set wsh = New WshShell

strCommand = Chr(34) & _
             ".bat_file_location" & _
             Chr(34)
lngErrorCode = wsh.Run(strCommand, _
                       WindowStyle:=0, _
                       waitonreturn:=True)
If lngErrorCode <> 0 Then
    MsgBox "Uh oh! Something went wrong with .bat file!"
    Exit Sub
End If
Set wsh = Nothing

此选项不起作用(lngErrorCode=0),我不太清楚为什么。我对其他通过 BTEQ 从 Teradata 数据库下载数据的 .bat 文件使用相同的方法,因此我假设 SQLcl/Oracle 数据库不想使用该方法。

你有什么想法可以克服这个问题吗?

.bat 文件代码:

sql user/password@//hostname:port/servicename @"path\code.sql"

任何建议将不胜感激。问候。

标签: excelvbaoraclebatch-filesqlcl

解决方案


推荐阅读