将SQL转换为VBAstring的函数

我在一个Excel工作表中构build了数百个SQL查询,每一个都放在一列1的单元格中。 我期望做的是从Excel中运行这些SQL语句中的每一个。

只是想知道如果有人知道一种方法将我所有的SQL转换成VBAstring,我可以遍历所有行来运行每个查询。

我发现这是我想要做的,但是有没有办法改变代码,以便读取excel单元而不是表单?

http://allenbrowne.com/ser-71.html

谢谢

编辑:这是我试图转换的示例SQL

SELECT TT.TEST_TABLE_ID, TT.TEST_TABLE_NO, TT.MEMBERSHIP_NUMBER, TT.TEST_TABLE_TYPE, from TEST_TABLE TT 

我认为,因为每个select是在自己的行,它会导致转换时出现问题。

编辑#2:这是我的代码,执行SQL

 Sub GetData() Dim Conn As New ADODB.Connection Dim RS As New ADODB.Recordset Dim cmd As New ADODB.Command Dim sqlText As String Dim Row As Long Dim Findex As Long Dim Data As Worksheet Dim X As Long Set Data = Sheets("Results") Data.Select Cells.ClearContents Conn.Open "PROVIDER=ORAOLEDB.ORACLE;DATA SOURCE=ORCL;USER ID=user;PASSWORD=password" cmd.ActiveConnection = Conn cmd.CommandType = adCmdText 'sqlText = How to reference Valid SQL cells cmd.CommandText = sqlText Set RS = cmd.Execute For X = 1 To RS.Fields.Count Data.Cells(1, X) = RS.Fields(X - 1).Name Next If RS.RecordCount < Rows.Count Then Data.Range("A2").CopyFromRecordset RS Else Do While Not RS.EOF Row = Row + 1 For Findex = 0 To RS.Fields.Count - 1 If Row >= Rows.Count - 50 Then Exit For End If Data.Cells(Row + 1, Findex + 1) = RS.Fields(Findex).Value Next Findex RS.MoveNext Loop End If Cells.EntireColumn.AutoFit End Sub 

在SQL文本部分,我希望能够引用我的SQL语句列。 我以为我需要转换它,但你们是正确的,如果引用它,我可以使用你的代码布拉德。

我试图把你的代码brad在我的'sqlText =如何引用有效的SQL单元格,但没有成功

这是我认为你需要的代码的开始。

我已经将SQL放在名为“SQL”的表格中,在列表A中。与此相关的问题是:(1)您将字段名称放在一行中,然后将数据返回到一行中。 这将需要每个SQL语句两行。 (2)我从表“SQL”复制了SQL语句,并放在“结果”的栏目A中(你提到你想把结果放在SQL String的右边)。(3)清除“结果”如果决定合并工作表,则需要注意不要擦除SQL。

 Option Explicit Sub Process_SQL_Strings() Dim cmd As New ADODB.Command Dim sqlText As String Dim Row As Long Dim Findex As Long Dim Data As Worksheet Dim iFldCt As Long Dim conn As ADODB.Connection Dim rs As ADODB.Recordset Dim sConn As String Dim lLastRow As Long Dim lRow As Long Set Data = Sheets("Results") Data.Select Cells.ClearContents conn.Open "PROVIDER=ORAOLEDB.ORACLE;DATA SOURCE=ORCL;USER ID=user;PASSWORD=password" cmd.ActiveConnection = conn cmd.CommandType = adCmdText '' Set conn = New ADODB.Connection '' sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _ '' "Data Source=C:\data\access\tek_tips.accdb;" & _ '' "Jet OLEDB:Engine Type=5;" & _ '' "Persist Security Info=False;" conn.Open sConn 'sqlText = How to reference Valid SQL cells lRow = 1 Do sqlText = Sheets("SQL").Range("A" & lRow) If sqlText = "" Then MsgBox "Finished processing " & lRow & " rows of SQL", vbOKOnly, "Finished" GoTo Wrap_Up End If Set rs = New ADODB.Recordset rs.Open sqlText, conn, adOpenStatic, adLockBatchOptimistic, adCmdText Data.Cells(lRow, 1) = sqlText If not rs.EOF then For iFldCt = 1 To rs.Fields.Count Data.Cells(lRow, 1 + iFldCt) = rs.Fields(iFldCt - 1).Name Next If rs.RecordCount < Rows.Count Then Data.Range("B" & lRow).CopyFromRecordset rs Else Do While Not rs.EOF Row = Row + 1 For Findex = 0 To rs.Fields.Count - 1 If Row >= Rows.Count - 50 Then Exit For End If Data.Cells(Row + 1, Findex + 1) = rs.Fields(Findex).value Next Findex rs.MoveNext Loop End If Cells.EntireColumn.AutoFit End If lRow = lRow + 1 Loop Wrap_Up: rs.Close Set rs = Nothing conn.Close Set conn = Nothing End Sub 

我正在使用这样的东西:

 Function SQLQueryRun(ByVal query As String, ByVal returnData As Boolean) As Variant Dim Conn As New ADODB.Connection Dim ADODBCmd As New ADODB.Command Dim ret As New ADODB.Recordset Conn.ConnectionString = "connection_string_here" Conn.Open ADODBCmd.ActiveConnection = Conn ADODBCmd.CommandText = query Set ret = ADODBCmd.Execute() If returnData Then If Not ret.EOF Then SQLQueryRun = ret.GetRows() Else SQLQueryRun = True End If Conn.Close Set Conn = Nothing Set ret = Nothing End Function 

如果第二个参数是False ,则函数不返回任何内容。 你是否期待查询运行的结果?
此外,我使用一个macros来创build查询/数据透视表从SQL剪贴板中包含的SQL,如果你有兴趣让我知道。

您需要创build一个到数据库的连接,并遍历所有的单元格,然后在每个单元格中执行您的代码。

您可以使用ADO来build立连接(需要添加对Microsoft ActiveX Data Objects 6.1 Library的引用)

你需要找出你的连接string ,打开一个连接,然后循环遍历所有的单元格,并在这些单元格中执行SQL。

 Dim cnn As New ADODB.Connection Dim connectionString As String Dim cmd As New ADODB.Command Dim c As Range, ws As Worksheet Dim rst as ADODB.Recordset connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data source=C:\Database3.accdb;Persist Security Info=False;" cnn.Open connectionString cmd.ActiveConnection = cnn For Each c In ws.Range() cmd.CommandText = c.Value set rst = cmd.Execute 'do what you need to with your new recordset before moving on to the next SELECT Next c