question

Edgar Almeida avatar image
0 Likes"
Edgar Almeida asked Jacob Gillespie edited

Is it possible to deconstruct an array in the Table.query?

Hello,

I was trying to do an query on a GlobalTable and my params are data into an array. The number of parameters are variable, so there is a way of "descontruct" the array into the params?
Ex:

Array arr = ['par1', 'par2', 'par3'];
Table result = Table.query("SELECT * FROM GlobalTable1 WHERE Col1 = $1 AND Col2 = $2 AND Col3 = $3", arr);

In the example above, 'par1' should enter in $1, 'par2' in $2 and so on.

FlexSim 21.2.2
queryarraysql queries
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
Jacob Gillespie avatar image
1 Like"
Jacob Gillespie answered Jacob Gillespie edited

It could be done like this:

Array arr = ["par1", "par2", "par3"];
string queryString = "SELECT * FROM GlobalTable1";
// build variable length query
if (arr.length > 0) {
    queryString += " WHERE ";
    for(int i = 1; i <= arr.length; i++) {
        queryString += "[Col " + i + "] = $1[" + i + "]";
        if (i != arr.length) {
            queryString += " AND ";
        }
    }
}
// makes a string like this "SELECT * FROM GlobalTable1 WHERE [Col 1] = $1[1] AND [Col 2] = $1[2] AND [Col 3] = $1[3]"
Table result = Table.query(queryString, arr);
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.