I don't have the language to phrase this accurately so
I will explain by example.
I have a table with 10 fields:
Table name = tblScore
Column names = id, Score1, Score2,...up to Score9
I have 20 records in the table with scores in each column.
I need to convert the table so that:
rather than having 1 record with 9 scores
I want 9 records with 1 score each (x 20 records)
In the end, I want a list of 180 records
(9 scores x 20 original records)
id, column name, score
The following code is the closest I can come:
--------------------------------------------------------
declare @colname nvarchar(50), @index int
set @index = 2 --start with column with first score
while @index < 10
begin
select @colname = COL_NAME(OBJECT_ID('tblScore'),@index)
from dbo.tblScore
--the following will not treat @colname as a column so I
--do not get the column value returned
select id, COL_NAME(OBJECT_ID('tblScore'),@index) AS ColumnName, @colname AS Score
from dbo.tblScore
set @index = @index + 1
end
---------------------------------------------------------
Please help - I don't how to use a variable in the select list and also don't know what keywords to use to search for faqs.
I will explain by example.
I have a table with 10 fields:
Table name = tblScore
Column names = id, Score1, Score2,...up to Score9
I have 20 records in the table with scores in each column.
I need to convert the table so that:
rather than having 1 record with 9 scores
I want 9 records with 1 score each (x 20 records)
In the end, I want a list of 180 records
(9 scores x 20 original records)
id, column name, score
The following code is the closest I can come:
--------------------------------------------------------
declare @colname nvarchar(50), @index int
set @index = 2 --start with column with first score
while @index < 10
begin
select @colname = COL_NAME(OBJECT_ID('tblScore'),@index)
from dbo.tblScore
--the following will not treat @colname as a column so I
--do not get the column value returned
select id, COL_NAME(OBJECT_ID('tblScore'),@index) AS ColumnName, @colname AS Score
from dbo.tblScore
set @index = @index + 1
end
---------------------------------------------------------
Please help - I don't how to use a variable in the select list and also don't know what keywords to use to search for faqs.