question

Tamilselvan avatar image
0 Likes"
Tamilselvan asked Tamilselvan commented

SQL query using the Label

I am trying to query a GlobalTable1 using SQL, but the column name is present in token.col in the token, how to access the token.col value in the sql query

FlexSim 23.1.1
sql query
5 |100000

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

Felix Möhlmann avatar image
0 Likes"
Felix Möhlmann answered Tamilselvan commented

Concatenate the label value and the query string.

"SELECT [" + token.col + "] FROM tableName WHERE..."
· 3
5 |100000

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

Tamilselvan avatar image Tamilselvan commented ·

Yeah it working fine Thanks for the Answer, Now, I just need to Query two column in WHERE which matches the value, can you please help out in this, i am attaching the file with thisFetch_Table_using_Token_and_SQL.fsm

0 Likes 0 ·
Felix Möhlmann avatar image Felix Möhlmann Tamilselvan commented ·

- The label on the token is called "Name", not "Col"

- When comparing a column to a string value, the string needs to be enclosed in single quotation marks within the query (WHERE [Name] == 'A'), so you need to add those to the query string in front of and after the label value.

- You need to convert the number value from "token.Num" to a string before you can insert it into the query like this (string.fromNum(token.Num))

- You only write WHERE once, then write multiple checks separated by AND or OR

Table result = Table.query("SELECT * FROM Test1 \
    WHERE [Name] = '"+token.Name+"' AND [Num] = "+string.fromNum(token.Num)+"");


With all that said, values that you want to compare columns to can more easily be passed in via the $-syntax. Inserting the label directly into the string is only needed if it represents a column name or SQL clause.

Table result = Table.query("SELECT * FROM Test1 WHERE [Name] = $1 AND [Num] = $2", token.Name, token.Num);

When the query is run, $1 will be replaced by the first value that is passed in after the query, $2 by the second and so on.

0 Likes 0 ·
Tamilselvan avatar image Tamilselvan Felix Möhlmann commented ·
Thanks for the Answer
0 Likes 0 ·
Tamilselvan avatar image
0 Likes"
Tamilselvan answered Felix Möhlmann commented

Can you please help me out with the example

· 1
5 |100000

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

Felix Möhlmann avatar image Felix Möhlmann commented ·
What is unclear about the example I provided? You type your query as you would normally until you would enter the column name. You then end the string, add the column name to it with ' + token.col', then you add the the rest of the query as a string again ( + "...").
0 Likes 0 ·