Hope this is easy (I just can't figure it in T-SQL)
I need to transform some data, moving iteratively through the columns of one table, to create a row for every value in a new table. The old table has column headings being months of the year. So I need to transform each record for that month into a record in the new table. I'm hoping to use some kind of column ordinal property to iterate through the base table in the style of:
set @month = 1
WHILE @month <13
BEGIN
INSERT INTO new_table(Item_Name, Item_Value)
SELECT Old_item_Name, column(@month) FROM Old_Table
set @month = @month + 1
END
Its the column(@month) bit which I can't sort out. I know other DBMSs allow you to reference columns in this way, and I refuse to be beaten on this one.
Many thanks
I need to transform some data, moving iteratively through the columns of one table, to create a row for every value in a new table. The old table has column headings being months of the year. So I need to transform each record for that month into a record in the new table. I'm hoping to use some kind of column ordinal property to iterate through the base table in the style of:
set @month = 1
WHILE @month <13
BEGIN
INSERT INTO new_table(Item_Name, Item_Value)
SELECT Old_item_Name, column(@month) FROM Old_Table
set @month = @month + 1
END
Its the column(@month) bit which I can't sort out. I know other DBMSs allow you to reference columns in this way, and I refuse to be beaten on this one.
Many thanks