1

I have a SELECT inside a stored procedures which outputs data into this format.

          A      B      C
rowkey    1      2      3

I need to transform it into something like this:

         key  value
rowkey   A    1
rowkey   B    2
rowkey   C    3

How should I go about transforming it into this?

I am not allowed to touch the SELECT statement, so I should find a way to transform it perhaps creating a temporary table.

1

1 Answer 1

1

This ought to get you going if you're in SQL 2005 or higher...

You would store the result of your internal SELECT into a table variable, then transform it with something like the below:

DECLARE @test TABLE (rowkey int identity, A int, B int, C int)

INSERT INTO @test (A, B, C)
VALUES (1, 2, 3)

SELECT rowkey, [Key], [Value]
FROM
(SELECT rowkey, A, B, C
FROM @test) t
UNPIVOT
( [Value] FOR [Key]  IN
    (A, B, C)
) AS u
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.