How to use dynamic column name in a query?

General

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.

eg:

TableA

IdColumn                          Expr

1                                       MyDollar * 1

2                                       (MyDollar * 1) - 5

3                                       MyDollar - HisDollar

TableB

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)

from 

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.

2 REPLIES
Enthusiast

Re: How to use dynamic column name in a query?

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.

Cheers,

Teradata Employee

Re: How to use dynamic column name in a query?

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