Monday, March 26, 2012

How to index through columns

How do you index through a set of Columns Programmatically using SQL. I have a Table all the columns have the same data for different dates. Column names are Col_0, Col_1, Col_2,....Col_100, Col_101. I need to perform the same calculation on each column to manipulate the data into a different table. Is it possible to do a While loop that changes the Column name in a SELECT statement. I have tried to do this but can't seem to get it to work. Please help !

You could set up a cursor using syscolumns.

Run this in QA after changing the table name:

select colid, name
from syscolumns
where id = (
select id
from sysobjects
where name = 'MyTableName')
order by colid

That should give you an idea of how to use that. You can dynamically build SQL using your cursor and execute it. There may be something more elegant, but I've used this successfully in the past.

|||Thank PDraigh I will try it

No comments:

Post a Comment