Named Query - Parametrized column names in select

I am creating a named query to only get certain columns from a table (mssql) based on the requirement. I can parametrized the where conditions but was not sure if there was a way to parametrized the column names. For example - in some cases I want column1 but in other cases I want column1, column2 and in some cases only column3. Or my option is to use system.db.runPrepQuery and create dynamic select statement.

Thank you

You can accomplish this with a QueryString parameter type, but you are re-opening the SQL injection hole closed by named queries in the first place, so don't accept user input blindly.
https://docs.inductiveautomation.com/display/DOC81/Named+Query+Parameters#NamedQueryParameters-QueryString

Or...you can create multiple Named Queries and call the query that you need when you need it.

1 Like

It sounds like you are scripting this, so your named query could return all of the columns, and then the resultant dataset could be subsequently filtered in the script using system.dataset.filterColumns

3 Likes