Transpose Rows to Columns

  • I have a problem with converting Rows to Column Names. I have created a table (CustomFields) where users can define user defined fields for a form. The structure of the table is (CustomFieldId, FieldType, FieldName). CustomFieldId is Primary Key. FieldType is the type of the field where user can select numeric, text, datetime etc. and The FieldName will become labels for a formview and as columns when shown in a gridview.

    I have a value table where I have 3 columns CustomValueId, CustomFieldId, CValue. This table has a foreign key relationship with CustomFields table. When user types in a value for a field this table will store that. Now is there anyway I can write a select statement that would give me a resultset with the CustomFieldName as the column names and the CValue as it's corresponding values.

    I know I can write C# code to add columns in a datatable but I was wondering if it can be done in some cool way with T-SQL

    Thanks in advance

    himadri

  • You don't appear to have anything to relate the given values to logical rows... have you posted all the columns that appear in these tables?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply