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. Retrive column names from temp table

Retrive column names from temp table

Scheduled Pinned Locked Moved Database
9 Posts 5 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.
  • S Offline
    S Offline
    SHAH MAULIK
    wrote on last edited by
    #1

    I am going to create dynamic temp table and I am looking for the columns available in that temp table for the same session id. you can refer code below for the same.

    ALTER procedure abc
    as
    create table #tmp_table
    (id int , name varchar(450) )

    select c.* ,t.name from tempdb.sys.columns c
    INNER JOIN tempdb.sys.tables t
    on c.object_id = t.object_id
    where t.name = '#tmp_table'
    -- don't use like '%tmp_table%'

    drop table #tmp_table
    return 0

    C Richard DeemingR 2 Replies Last reply
    0
    • S SHAH MAULIK

      I am going to create dynamic temp table and I am looking for the columns available in that temp table for the same session id. you can refer code below for the same.

      ALTER procedure abc
      as
      create table #tmp_table
      (id int , name varchar(450) )

      select c.* ,t.name from tempdb.sys.columns c
      INNER JOIN tempdb.sys.tables t
      on c.object_id = t.object_id
      where t.name = '#tmp_table'
      -- don't use like '%tmp_table%'

      drop table #tmp_table
      return 0

      C Offline
      C Offline
      CHill60
      wrote on last edited by
      #2

      From this article[^]

      Quote:

      Another oddity of the local temporary table (and the local temporary stored procedure) is that it has a different name in the metadata to the one you give it in your routine or batch. If the same routine is executed simultaneously by several processes, the Database Engine needs to be able to distinguish between the identically-named local temporary tables created by the different processes. It does this by adding a numeric string to each local temporary table name left-padded by underscore characters. Although you specify the short name such as #MyTempTable, what is actually stored in TempDB is made up of the table name specified in the CREATE TABLE statement and the suffix. Because of this suffix, local temporary table names must be 116 characters or less.

      If you remove the where from your code you will see that the table name is actually

      #tmp_table__________________________________________________________________________________________________________000000000002

      So you do need to use a like clause but not the one you have commented out. Like this

      where t.name LIKE '#tmp_table%'

      Richard DeemingR S 2 Replies Last reply
      0
      • C CHill60

        From this article[^]

        Quote:

        Another oddity of the local temporary table (and the local temporary stored procedure) is that it has a different name in the metadata to the one you give it in your routine or batch. If the same routine is executed simultaneously by several processes, the Database Engine needs to be able to distinguish between the identically-named local temporary tables created by the different processes. It does this by adding a numeric string to each local temporary table name left-padded by underscore characters. Although you specify the short name such as #MyTempTable, what is actually stored in TempDB is made up of the table name specified in the CREATE TABLE statement and the suffix. Because of this suffix, local temporary table names must be 116 characters or less.

        If you remove the where from your code you will see that the table name is actually

        #tmp_table__________________________________________________________________________________________________________000000000002

        So you do need to use a like clause but not the one you have commented out. Like this

        where t.name LIKE '#tmp_table%'

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        That will return *all* copies of that temporary table for *all* sessions. Filtering by ``object_id`` would probably work better. :)


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        C 1 Reply Last reply
        0
        • S SHAH MAULIK

          I am going to create dynamic temp table and I am looking for the columns available in that temp table for the same session id. you can refer code below for the same.

          ALTER procedure abc
          as
          create table #tmp_table
          (id int , name varchar(450) )

          select c.* ,t.name from tempdb.sys.columns c
          INNER JOIN tempdb.sys.tables t
          on c.object_id = t.object_id
          where t.name = '#tmp_table'
          -- don't use like '%tmp_table%'

          drop table #tmp_table
          return 0

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Try filtering by ``object_id``: ``` select c.* ,t.name from tempdb.sys.columns c INNER JOIN tempdb.sys.tables t on c.object_id = t.object_id where t.object_id = OBJECT_ID('tempdb..#tmp_table') ```


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          M S 2 Replies Last reply
          0
          • Richard DeemingR Richard Deeming

            That will return *all* copies of that temporary table for *all* sessions. Filtering by ``object_id`` would probably work better. :)


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            C Offline
            C Offline
            CHill60
            wrote on last edited by
            #5

            Yep - good point :thumbsup:

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              Try filtering by ``object_id``: ``` select c.* ,t.name from tempdb.sys.columns c INNER JOIN tempdb.sys.tables t on c.object_id = t.object_id where t.object_id = OBJECT_ID('tempdb..#tmp_table') ```


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

              I learn something most days, got my 5!

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              • C CHill60

                From this article[^]

                Quote:

                Another oddity of the local temporary table (and the local temporary stored procedure) is that it has a different name in the metadata to the one you give it in your routine or batch. If the same routine is executed simultaneously by several processes, the Database Engine needs to be able to distinguish between the identically-named local temporary tables created by the different processes. It does this by adding a numeric string to each local temporary table name left-padded by underscore characters. Although you specify the short name such as #MyTempTable, what is actually stored in TempDB is made up of the table name specified in the CREATE TABLE statement and the suffix. Because of this suffix, local temporary table names must be 116 characters or less.

                If you remove the where from your code you will see that the table name is actually

                #tmp_table__________________________________________________________________________________________________________000000000002

                So you do need to use a like clause but not the one you have commented out. Like this

                where t.name LIKE '#tmp_table%'

                S Offline
                S Offline
                Smart003
                wrote on last edited by
                #7

                instead of separate where condition use the condition in join statement only

                select c.* ,t.name from tempdb.sys.columns c
                INNER JOIN tempdb.sys.tables t
                on (c.object_id = t.object_id
                and t.name = '#tmp_table');

                C 1 Reply Last reply
                0
                • S Smart003

                  instead of separate where condition use the condition in join statement only

                  select c.* ,t.name from tempdb.sys.columns c
                  INNER JOIN tempdb.sys.tables t
                  on (c.object_id = t.object_id
                  and t.name = '#tmp_table');

                  C Offline
                  C Offline
                  CHill60
                  wrote on last edited by
                  #8

                  Same problem will apply for the same reason I gave earlier - the table name does not equal '#tmp_table' so for this suggestion to work the ON clause will need to have

                  and t.name LIKE '#tmp_table**%**'

                  or

                  and t.object_id = OBJECT_ID('tempdb..#tmp_table')

                  (Credit to @Richard Deeming)

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Try filtering by ``object_id``: ``` select c.* ,t.name from tempdb.sys.columns c INNER JOIN tempdb.sys.tables t on c.object_id = t.object_id where t.object_id = OBJECT_ID('tempdb..#tmp_table') ```


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    S Offline
                    S Offline
                    SHAH MAULIK
                    wrote on last edited by
                    #9

                    thanks .. It's working fine..

                    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