提供程序OraOLEDB.Oracle不会为vba返回正确的结果

时间:2013-03-01 19:13:03

标签: vba vb6 oracle11g provider

我有这个.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

2 个答案:

答案 0 :(得分:0)

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

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

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')"

答案 1 :(得分:0)

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

connectionString =“ PROVIDER = OraOLEDB.Oracle; Persist Security Info = True; UseSessionFormat = True; USER ID = XXXX; PASSWORD = XXXX;数据源=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL = TCP)(HOST)) = XXX)(PORT = XXXX)))(CONNECT_DATA =(SID = XXXX)(SERVER = DEDICATED)))“”