• robert.sterbal 56890 (7/7/2015)


    Is there a simple way to get a single row table with variable numbers of columns to output with 2 columns?

    i.e.

    colA colB colC

    1 5 23

    colA 1

    colB 2

    colC 3

    and handle an extra column easily:

    colA colB colC colD

    1 5 23 743

    colA 1

    colB 2

    colC 3

    colD 743

    "Simple" is a relative term. Which version of SQL Server are you using? A relatively simple version would entail reading syscolumns for the table and then dynamic SQL to construct a query from the results.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden