I have a SQL Server 2008 R2 table with the following Schema
DECLARE @AttributeTable TABLE
(
Code1 nvarchar(50),
Value1 nvarchar(50),
Code2 nvarchar(50),
Value2 nvarchar(50),
Stock int
);
With values like:
Color, Red, Size, Large, 15
Color, Blue, Size Large, 5
Size, Large, Color, Green, 4
And I am looking for a way to reorder the columns for each row (dynamically, not in the table itself) so that the result of a query would be
Color, Red, Size, Large, 15
Color, Blue, Size Large, 5
Color, Green, Size, Large, 4
But I can’t think of any way to do this without resorting to creating a .NET function, which just seems like overkill.
I admit this is a failed architecture, but the schema belongs to a 3rd party ERP that I am unable to change.
And finally, if anyone has a good idea for a title for this question, please feel free to edit (or comment and I will change it)
EDIT:
The real table this example is based on has 6 different key-value pairs instead of two and the ‘Code’ value is dynamic (The current database has about 45 different code values).
This handles 3 generic Code/Value pairs and correctly re-orders the output. It is not perfect obviouly, but it solved my needs.
The first query finds the code values and puts them in the order of the first item in the table. It would not be hard to extend this as a function or stored procedure and pass in an order.
The second query uses PaulBailey’s solution to order the pairs correctly.