Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. T-SQL Table Variable - Create a column for each row in a table

T-SQL Table Variable - Create a column for each row in a table

Scheduled Pinned Locked Moved Database
databasetoolsperformancetutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kilkfoe1
    wrote on last edited by
    #1

    Hello, I need to write a script that creates a table variable with a column for every instance of a row in a table. The data in the table it pulls from may change from time to time, so I'm looking for a dynamic approach. Example table:

    ColumnA: ColumnB:
    1 Hello
    2 World

    Then my table variable declaration should look like:

    DECLARE @TableVariable TABLE
    (
    Hello varchar(20),
    World varchar(20)
    )

    Can anyone think of a performance-friendly solution to this? Thanks, Kevin

    S T M 3 Replies Last reply
    0
    • K kilkfoe1

      Hello, I need to write a script that creates a table variable with a column for every instance of a row in a table. The data in the table it pulls from may change from time to time, so I'm looking for a dynamic approach. Example table:

      ColumnA: ColumnB:
      1 Hello
      2 World

      Then my table variable declaration should look like:

      DECLARE @TableVariable TABLE
      (
      Hello varchar(20),
      World varchar(20)
      )

      Can anyone think of a performance-friendly solution to this? Thanks, Kevin

      S Offline
      S Offline
      SilimSayo
      wrote on last edited by
      #2

      I thing you should dynamically create an sql string that creates the table. On the other hand, why would you want a to create a column for each row? suppose. Keep in mind that SQL Server has a max column limit of 255(or 256) per table.

      1 Reply Last reply
      0
      • K kilkfoe1

        Hello, I need to write a script that creates a table variable with a column for every instance of a row in a table. The data in the table it pulls from may change from time to time, so I'm looking for a dynamic approach. Example table:

        ColumnA: ColumnB:
        1 Hello
        2 World

        Then my table variable declaration should look like:

        DECLARE @TableVariable TABLE
        (
        Hello varchar(20),
        World varchar(20)
        )

        Can anyone think of a performance-friendly solution to this? Thanks, Kevin

        T Offline
        T Offline
        Tim Carmichael
        wrote on last edited by
        #3

        Kevin Leeds wrote:

        Can anyone think of a performance-friendly solution to this?

        Yes.. I think. I encounted a similar issue this week and solved it by using a pivot table. There is an excellent article on hear that gives a workable example. Pivot two or more columns in SQL Server 2005[^] Hope this helps... Tim

        M 1 Reply Last reply
        0
        • T Tim Carmichael

          Kevin Leeds wrote:

          Can anyone think of a performance-friendly solution to this?

          Yes.. I think. I encounted a similar issue this week and solved it by using a pivot table. There is an excellent article on hear that gives a workable example. Pivot two or more columns in SQL Server 2005[^] Hope this helps... Tim

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Glad it was useful. Always a buz when an article is used as reference (by someone other than the author :-O )

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • K kilkfoe1

            Hello, I need to write a script that creates a table variable with a column for every instance of a row in a table. The data in the table it pulls from may change from time to time, so I'm looking for a dynamic approach. Example table:

            ColumnA: ColumnB:
            1 Hello
            2 World

            Then my table variable declaration should look like:

            DECLARE @TableVariable TABLE
            (
            Hello varchar(20),
            World varchar(20)
            )

            Can anyone think of a performance-friendly solution to this? Thanks, Kevin

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            A pivot may do the job for you, organsing it will be a challenge. Dynamic SQL is more straight forward EXCEPT if you use EXEC (@SQL) to create the table var it is created in a different proc domain and therefore not available to you. Creating a global ##Table will fix this but it is not recommended in a frequently called proc.

            Never underestimate the power of human stupidity RAH

            K 1 Reply Last reply
            0
            • M Mycroft Holmes

              A pivot may do the job for you, organsing it will be a challenge. Dynamic SQL is more straight forward EXCEPT if you use EXEC (@SQL) to create the table var it is created in a different proc domain and therefore not available to you. Creating a global ##Table will fix this but it is not recommended in a frequently called proc.

              Never underestimate the power of human stupidity RAH

              K Offline
              K Offline
              kilkfoe1
              wrote on last edited by
              #6

              Thanks for the responses. I wish I could take the time to test these, but we have decided to move on - not making this a dynamic proc.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups