我有一种情况,我想将值从excel工作表传递给我的SQL查询
我已经为图片1表定义了名称=>螺柱
像这样传递下面的查询时,它无法运行我的VB脚本
select * from Student_Details where ID in (Stud) group by COURSE
我在Oracle数据库上触发的实际查询给我输出
select * from Student_Details where ID in (123,201,234,345,678,70,71,51,34) group by COURSE
下面是我要实现的代码
Sub FetchRecordSetQuery()
Dim DBcon As ADODB.Connection
Dim DBrs As ADODB.Recordset
Set DBcon = New ADODB.Connection
Set DBrs = New ADODB.Recordset
Dim DBHost As String
Dim DBPort As String
Dim DBsid As String
Dim DBuid As String
Dim DBpwd As String
Dim DBQuery As String
Dim ConString As String
Dim intColIndex As Integer
On Error GoTo err
' DB connectivity details. Pass the correct connectivity details here
DBHost = "ABC"
DBPort = "XXXX"
DBsid = "XXXXX"
DBuid = "XXXXX"
DBpwd = "XXXXXXX"
'Connection string to connect to Oracle using SID
ConString = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=" & DBHost & ")(PORT=" & DBPort & "))" & _
"(CONNECT_DATA=(SID=" & DBsid & "))); uid=" & DBuid & "; pwd=" & DBpwd & ";"
'Open the connection using Connection String
DBcon.Open (ConString) 'Connecion to DB is made
DBQuery = "select * from Student_Details where ID in (Stud) group by COURSE"
'below statement will execute the query and stores the Records in DBrs
DBrs.Open DBQuery, DBcon
If Not DBrs.EOF Then 'to check if any record then
' Spread all the records with all the columns
' in your sheet from Cell A2 onward.
Sheets("Sheet1").Range("A2").CopyFromRecordset DBrs
'Above statement puts the data only but no column
'name. hence the below for loop will put all the
'column names in your excel sheet.
For intColIndex = 0 To DBrs.Fields.Count - 1 ' recordset fields
Sheets("Sheet1").Cells(1, intColIndex + 1).Value = DBrs.Fields(intColIndex).Name
Next
End If
'Close the connection
DBcon.Close
Exit Sub
err:
MsgBox "Following Error Occurred: " & vbNewLine & err.Description
DBcon.Close
End Sub