0
創建動態選擇語句我當前正在創建一個臨時文件來保存查詢(由於使用的連接數和解碼數量,30k +個字符)。我想使用這個具有完整select語句的臨時文件將數據返回到excel。運行SQL使用Excel中的VBA從.sql文件中選擇查詢
使用我當前的代碼我只能運行一個簡短的選擇語句。我創建的字符串似乎由於某種原因被截斷。
下面是我正在使用的代碼片段創建文件,並且當前嘗試運行相同的字符串。
' Set file details
fileDir = "C:\temp\"
filePath = "C:\temp\" & node & "_SRO_TCs.sql"
'check if directory exists, if not create it
If Dir(fileDir, cbDirectory) = "" Then
MkDir fileDir
End If
' open the file
Open filePath For Output As #1
'Write to file
outputText = sqlQuery3
Print #1, outputText
'open connection
sqlCon.ConnectionString = Conn
'Cn.CursorLocation = adUseClient
sqlCon.Open
'set and execute sql command
Set sqlCommand.ActiveConnection = sqlCon
sqlCommand.CommandText = sqlQuery3
sqlCommand.CommandType = adCmdText
sqlCommand.Execute
'open recordset
Set sqlRecordSet.ActiveConnection = sqlCon
sqlRecordSet.Open sqlCommand
'copy data to excel
ActiveSheet.Range("A1").CopyFromRecordset (sqlRecordSet) <<<< This is where i get an error returned when stepping through the code - "Run-time error '91': Object variable or With block variable not set"
'close connections
sqlRecordSet.Close
sqlCon.Close
'Close file
Close #1
當我檢查創建的文件時,它有一個工作的sql select語句。我希望能夠運行此文件或字符串。請幫忙!
您正在打開輸出文件並將sqlQuery3寫入文件。你的意思是將文件讀入名爲sqlQuery3的字符串變量嗎? – Fionnuala
@Remou sqlQuery3是正在寫入文件的查詢,它已包含選擇字符串。我想用一種方法來使用創建的文件而不是sqlQuery3,因爲當我嘗試通過sqlCommand傳遞它時,sqlQuery3會被截斷。 –
@Tony - 我想你錯了那個截斷。你爲什麼認爲你的SQL被截斷了?我遇到過的唯一限制是單個Oracle SQL查詢的最大大小,它大大超過了256個字符。 –