Excel VBA - Oracle数据库连接 - 更新查询的执行不起作用

时间:2017-05-25 10:14:59

标签: excel oracle vba

我正在使用Excel VBA连接Oracle DB并运行一些查询。我运行了一些复杂的选择查询,它工作正常。但它无法运行简单的更新查询。当我运行查询时,应用程序挂起并且不执行查询(我已经检查了数据库,但没有看到任何更新)。当我使用SQL开发人员执行它时,更新查询运行正常。请帮忙。以下是代码 -

Sub checkupd()

strDriver = "Microsoft ODBC for Oracle"
strHost = ThisWorkbook.Sheets("Querygen").Cells(3, 17).Value
strPort = ThisWorkbook.Sheets("Querygen").Cells(4, 17).Value
strUser = ThisWorkbook.Sheets("Querygen").Cells(5, 17).Value
strPassword = ThisWorkbook.Sheets("Querygen").Cells(6, 17).Value
strSID = ThisWorkbook.Sheets("Querygen").Cells(7, 17).Value

'Connection String
strCon = strCon & "CONNECTSTRING=(DESCRIPTION="
strCon = strCon & "(ADDRESS=(PROTOCOL=TCP)"
strCon = strCon & "(HOST=" & strHost & ")(PORT=" & strPort & "))"
strCon = strCon & "(CONNECT_DATA=(SID=" & strSID & "))); "
strCon = strCon & "uid=" & strUser & ";pwd=" & strPassword & ";"

strQueryup = "UPDATE DM_RESERVE SET COL1 = 'new3'"
strQueryup = strQueryup & " WHERE PROD IN ('123','456')"


Set conn = CreateObject("ADODB.Connection")
conn.Open strCon

conn.Execute strQueryup 


MsgBox ("Update Query executed")


conn.Close


Set conn = Nothing
End Sub

3 个答案:

答案 0 :(得分:0)

您是否遇到任何语法问题,我可以看到空间问题。你能试试吗

strQueryup = "UPDATE DM_RESERVE SET COL1 = 'new3'"
strQueryup = strQueryup & " WHERE PROD IN ('123','456')"

答案 1 :(得分:0)

驱动程序" Microsoft ODBC for Oracle"多年来deprecated。安装ODBC driver from Oracle并重试。

答案 2 :(得分:0)

感谢大家的帮助。 我能够通过在Update查询之前和之后使用“COMMIT”解决此问题。

...
conn.Execute "COMMIT"
conn.Execute strQueryup 
conn.Execute "COMMIT"
....