How to dynamically pass column name into the SQL Query?

How to dynamically pass column name into the SQL Query?

asb_praneeth
Not applicable
51 Views
3 Replies
Message 1 of 4

How to dynamically pass column name into the SQL Query?

asb_praneeth
Not applicable

[ FlexSim 17.0.13 ]

Hello

I am trying to write a query in which I want to query certain column of the table based on Item name. This is the following query which I am trying but it is not working

string myName = item.name;
query("SELECT * FROM $1 WHERE [$3] > 0 AND ([Operator] = $2 OR [Operator] = 'AnyOne')"
,reftable("ML1_TaskListTable"),i,myName);
TaskDescriptionPartAPartBPartC
1sdf0.2500.25
2d1.2520
3a003.06
4h31.750

Thanks in advance,

Praneeth Akondi

0 Likes
Accepted solutions (1)
52 Views
3 Replies
Replies (3)
Message 2 of 4

Jacob_Gillespie
Autodesk
Autodesk
Accepted solution

Just insert the string using the + operator:

string myName = item.name;
query("SELECT " + myName + " FROM $1 WHERE [Operator] = $2 OR [Operator] = 'AnyOne'"
,reftable("ML1_TaskListTable"),i);
Message 3 of 4

mischa_spelt
Advisor
Advisor

A little safer is

"SELECT [" + myName + "] FROM $1"

in case myName happens to contain a space.

Message 4 of 4

asb_praneeth
Not applicable

Thank you...! It is very basic... ! But I couldnt get it at the time of coding...!

0 Likes