0

我有这个 .xls vba 代码,我试图在其中检查记录计数并基于该代码决定进一步的逻辑。

这个 vba 在 Provider = MSDAORA 上运行良好,但是当我更改为 Provider = OraOLEDB.Oracle 这个 vba 检查查询总是返回记录计数 = 0

使用 OraOLEDB.Oracle 的所有其他查询都可以正常工作。

此处附上代码。

提前感谢您在这里指出问题。

Public Function CheckJob(BusDate As String, JobName As String) As Boolean
Dim Cnt As ADODB.Connection, rst As ADODB.Recordset
Dim cmdGetComp As New ADODB.Command   
Dim RecCount As Integer
Dim rng As Range
Dim StConn As String
Dim Sql As String

StConn = strETDDBConnStringADO()'This is connection string with all credentials
Set Cnt = New ADODB.Connection
Set rst = New ADODB.Recordset
Set cmdGetComp = New ADODB.Command

Sql = "Select Count(*) from XYZ_DBO.TABLE_STATUS where BUSINESS_DATE='XXXXXX'"


With Cnt
.Open (StConn)

With cmdGetComp
    .ActiveConnection = Cnt ' Reference to a Connection object.
    .CommandType = adCmdText
    .CommandText = Sql
     Set rst = .Execute() ' Set the Recordset to the results of executing the query
End With

RecCount = rst(0)  'rst(0) always return value of 0 even if manual query have 1 record
.Close
End With
If RecCount > 0 Then   
CheckJob = True
Else
CheckJob = False
End If
End Function
4

2 回答 2

0

MSDAORA 似乎对某些转换更直观,而 OraOLEDB.Oracle 更明确。

因此,虽然 MSDAORA 将 '01-JAN-2015' 理解为与日期列比较时的有效日期,但 OraOLEDB.Oracle 要求将字符串转换为日期,例如 to_date('01-JAN-2015', 'DD -MON-YYYY')。

TLDR:

Sql = "Select Count(*) from XYZ_DBO.TABLE_STATUS where BUSINESS_DATE='XXXXXXXX'"

应该变成类似的东西(取决于 BUSINESS_DATE 数据类型)

Sql = "Select Count(*) from XYZ_DBO.TABLE_STATUS where BUSINESS_DATE= to_date('XXXXXXXX','MMDDYYYY')"
于 2015-05-15T15:38:28.993 回答
0

在连接字符串中使用 UseSessionFormat=True,因此它始终使用 Oracle 默认日期格式。

connectionString = "PROVIDER=OraOLEDB.Oracle;Persist Security Info=True;UseSessionFormat=True;USER ID=XXXX;PASSWORD=XXXX;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=XXX) (PORT=XXXX)))(CONNECT_DATA=(SID=XXXX)(SERVER=DEDICATED)))""

于 2018-06-19T14:34:10.300 回答