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:

  1. Array arr = ['par1', 'par2', 'par3'];
  2. 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:

  1. Array arr = ["par1", "par2", "par3"];
  2. string queryString = "SELECT * FROM GlobalTable1";
  3. // build variable length query
  4. if (arr.length > 0) {
  5. queryString += " WHERE ";
  6. for(int i = 1; i <= arr.length; i++) {
  7. queryString += "[Col " + i + "] = $1[" + i + "]";
  8. if (i != arr.length) {
  9. queryString += " AND ";
  10. }
  11. }
  12. }
  13. // makes a string like this "SELECT * FROM GlobalTable1 WHERE [Col 1] = $1[1] AND [Col 2] = $1[2] AND [Col 3] = $1[3]"
  14. 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.