How to convert rows of data into table stucture

  • Hi all,

    This is very simple but I don't have the know how...

    I want to take a column (field) and make its rows, between 5 and 15 rows, the structure of a new table.

    Any help is deeply appreciated...

  • Not sure what you mean,

    Can you please post more information.

     

  • I have a query something like this:

    Select descript1 from table1 join table2 on .... where criteria='X'

    descript1 (column header)

    row 1 AO

    row 2 INU

    row 3 IMP

    ...

    row N TRN

    I want to create a temp table with these values as the column names and then be able to INSERT data coming from other tables.

    AOINUIMP…TRN

    5381

    2467

    The only problem is that the number of columns depend on the nuumber of rows in the query.

  • When creating a table, you need to know not only column names, but also data types.

    Generally, creating table structure based on data in another table is not considered good practice. You should always know what table you want to create and state that directly - not read column names from a table. If you really insist on this, it would require dynamic SQL, where you compile the statement in a varchar variable and then execute it after it is finished.

  • This looks like a pivot/crosstab request, and there are several articles out there on the subject.

    I recommend you take a look at them...

    http://www.sqlservercentral.com/columnists/plarsson/pivottableformicrosoftsqlserver.asp

    http://www.sqlteam.com/item.asp?ItemID=2955

    http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspx

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

Viewing 5 posts - 1 through 4 (of 4 total)

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