All Forums General
jacobjonz 2 posts Joined 09/10
09 Jan 2014
How to use dynamic column name in a query?

Hi All,


I am trying to get the bteq script to dynamically select the column name [actually an expression using one ore more column names] to query using the value stored in another column.


I cannot use CASE statements as the user may want to come up later with different expressions.








IdColumn                          Expr




1                                       MyDollar * 1


2                                       (MyDollar * 1) - 5


3                                       MyDollar - HisDollar








IdColumn                           MyDollar                           HisDollar


1                                          10                                       20


2                                          20                                       20


3                                          30                                       35



The functionality Iam trying to achieve is:


Select A.IdColumn, Evaluated value of (A.EXpr)


TableA A

inner join TableB B on A.IdColumn = B.IdColumn


I am not sure if I can create Macros/Store procedures, etc.

So, I am prefering a solution in SQL itself if possible.

But, I would still like to know the other solutions in case a SQL only solution is impossible


Any help is appreciated.

Thanks in advance.


Raja_KT 1246 posts Joined 07/09
09 Jan 2014

Hi Jacob,
My thought of  bteq as:
export the result set from B. Then use "using" at the time of import to resolve  the required query.
Please let me know.

Raja K Thaw
My wiki:
Street Children suffer not by their fault. We can help them if we want.

Adeel Chaudhry 773 posts Joined 04/08
03 Feb 2014

You can implement it using an SP. Which version of TD you are using?

-- If you are stuck at something .... consider it an opportunity to think anew.

You must sign in to leave a comment.