2

Is it possible to use an array or dictionary as the table in a SQL statement.

e.g. strSQL = "SELECT * FROM " & myArray & ""

Thanks in advance.

6
  • Not in VBA. VB.NET has Linq which is like SQL. SQL is for querying databases, not local variables. What would you expect the output to be? If you already have an array what do you need the SQL for? Commented Jun 16, 2016 at 13:48
  • Paste the array to an empty sheet and then use ADO to query the sheet. Here is a solution with sample code for your consideration: stackoverflow.com/questions/3955061/… Commented Jun 16, 2016 at 13:51
  • That was only a rough example the actually sql would have WHERE and GROUP BY clauses, possibly with multiple sql statements being run across the data. Commented Jun 16, 2016 at 13:52
  • 1
    What do you want to do at all? What would be the desired query? Normally you should use Join to link tables. If you would prepend the tables comma separated you would create a cartesian product of all possible combinations of rows between all tables. Commented Jun 16, 2016 at 13:53
  • Thanks Ralph but this is being deployed as an XLA so i wanted to keep writing to a sheet to a minimum Commented Jun 16, 2016 at 13:54

3 Answers 3

2

Expanding upon the idea provided by @Nathan_Sav the following code should to it:

Dim a(3) As String

a(0) = "1 as C1"
a(1) = 2
a(2) = 3
a(3) = 4

Debug.Print "select * from (SELECT " & Join(a, " UNION ALL SELECT ") & ") as tmp"

Update:

Here is a short sub to manually concatenate / construct the necessary string:

Option Explicit

Sub tmpTest()

Dim strSQL As String
Dim varArray As Variant
Dim lngRow As Long, lngColumn As Long

varArray = ThisWorkbook.Worksheets(1).Range("A1:G12")

strSQL = "select * from "
strSQL = strSQL & "(select "
For lngRow = LBound(varArray, 1) + 1 To UBound(varArray, 1)
    For lngColumn = LBound(varArray, 2) To UBound(varArray, 2)
        'First row must contain column names
        If lngRow = LBound(varArray, 1) + 1 Then
            strSQL = strSQL & varArray(lngRow, lngColumn) & _
                " as [" & varArray(lngRow - 1, lngColumn) & "], "
        Else
            strSQL = strSQL & varArray(lngRow, lngColumn) & ", "
        End If
        'Beyond the last column a UNION ALL SELECT must be added
        '  except for the last row
        If lngColumn = UBound(varArray, 2) And _
            lngRow < UBound(varArray, 1) Then
                strSQL = Mid(strSQL, 1, Len(strSQL) - 2) & _
                    " union all select "
        End If
    Next lngColumn
Next lngRow
strSQL = Mid(strSQL, 1, Len(strSQL) - 2) & ") as tmp"
Debug.Print strSQL

End Sub

The above code assumes the data to be on sheet1 in the range A1:G12 where the first row contains the columns headers.

Sign up to request clarification or add additional context in comments.

2 Comments

thx ralph, just to complicate matters the array is 2 dimensional, so i get a subscript out of range error when i try. Is it possible for this to work on a 2 dimensional array. thx for all your help so far (everyone).
Sorry Ralph i'm kinda new at this and don't know the etiquette, happy to be pointed in the right direction on that.
2

SQL array like search function:

SELECT TABLEARRAY.*
FROM (
    SELECT P.*
    FROM Person P
    WHERE P.id IN (1, 2, 3, 4, 18) --This would be your array with ID's
     ) AS TABLEARRAY

Comments

2
Dim a(3) As Variant

a(0) = 1
a(1) = 2
a(2) = 3
a(3) = 4

Debug.Print "select * from xyz where id in (" & Join(a, ",") & ")"

4 Comments

The array is the table and not a where-clause. If anything you will have to construct the table with UNION ALL. So, maybe something like this: Debug.Print "select * from (" & Join(a, " UNION ALL ") & ") as tmp"
What do you mean Ralph
Debug.Print "select * from (SELECT " & Join(a, " UNION ALL SELECT ") & ") as tmp" is almost what you want. But it is still missing a column name.
Ok, So say the columns are "A", "B", "C", these are defined in the first "row" of the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.