Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:28:00+00:00 2026-06-07T04:28:00+00:00

I have a SQL Server 2008 R2 table with the following Schema DECLARE @AttributeTable

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:28:02+00:00Added an answer on June 7, 2026 at 4:28 am
    • If you upvote this answer, please upvote PaulBailey’s as his answer made mine possible *

    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.

           DECLARE @ItemCode nvarchar(50) = 'ITEM-000001'
           DECLARE @Code1 nvarchar(50)
           DECLARE @Code2 nvarchar(50)
           DECLARE @Code3 nvarchar(50)
    
            SELECT TOP 1
                   @Code1 = ii.AttributeCode1
                 , @Code2 = ii.AttributeCode2
                 , @Code3 = ii.AttributeCode3
              FROM @AttributeTable ii
             WHERE ii.ItemCode = @ItemCode
    
            SELECT ii.ItemCode
                 , @Code1 as [AttributeCode1]
                 , CASE WHEN ii.AttributeCode1 = @Code1 THEN ii.Attribute1 
                        WHEN ii.AttributeCode2 = @Code1 THEN ii.Attribute2 
                        WHEN ii.AttributeCode3 = @Code1 THEN ii.Attribute3
                        ELSE null END as [Attribute1]
                 , @Code2 as [AttributeCode2]
                 , CASE WHEN ii.AttributeCode1 = @Code2 THEN ii.Attribute1 
                        WHEN ii.AttributeCode2 = @Code2 THEN ii.Attribute2 
                        WHEN ii.AttributeCode3 = @Code2 THEN ii.Attribute3
                        ELSE null END as [Attribute2]
                 , @Code3 as [AttributeCode3]
                 , CASE WHEN ii.AttributeCode1 = @Code3 THEN ii.Attribute1 
                        WHEN ii.AttributeCode2 = @Code3 THEN ii.Attribute2 
                        WHEN ii.AttributeCode3 = @Code3 THEN ii.Attribute3
                        ELSE null END as [Attribute3]
              FROM @AttributeTable ii
             WHERE ii.ItemCode = @ItemCode
          ORDER BY [AttributeCode1], [Attribute1]
                 , [AttributeCode2], [Attribute2]
                 , [AttributeCode3], [Attribute3]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have sql server 2008 db table FILE_DETAILS in following format. ID FileName Filesize_in_MB
I have a following table in SQL Server 2008 database: CREATE TABLE [dbo].[Actions]( [ActionId]
I have the following table in a SQL Server 2008 database with lots of
I have a SQL Server 2008 database with the following information in a table:
In SQL Server 2008, I have a table with following columns and data Date
Let's say I have the following table in SQL Server 2008: ProfileID int //identity;
Consider the following table in SQL Server 2008: LanguageCode varchar(10) Language nvarchar(50) LanguageCode participates
I have SQL Server 2008 with a table called ProductCategories designed like this: Id
I have a Money column in my SQL Server 2008 table. In my below
i have four tables like below in sql server 2008 : TABLE 1 ->

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.