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. Variable Table Identifiers

Variable Table Identifiers

Scheduled Pinned Locked Moved Database
sharepointdatabasetutorialquestion
4 Posts 2 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.
  • J Offline
    J Offline
    Jeff J
    wrote on last edited by
    #1

    I am creating tables dynamically using stored procedures, and would like to use a parameter variable as the table_name identifier. The SPs are implemented in T-SQL scripts. I am currently using literal identifiers as usual, then renaming the tables via sp_rename. For example (with SET QUOTED_IDENTIFIER ON):

    CREATE PROC spCreateTable (@TableName sysname)
    AS
    BEGIN
    CREATE TABLE "_TempTableName_" (
    /* ... */
    )

    EXEC sp_rename '_TempTableName_', @TableName --rename to desired name
    END
    GO

    Does anyone know how I could use @TableName directly or indirectly in the CREATE TABLE statement?

    R 1 Reply Last reply
    0
    • J Jeff J

      I am creating tables dynamically using stored procedures, and would like to use a parameter variable as the table_name identifier. The SPs are implemented in T-SQL scripts. I am currently using literal identifiers as usual, then renaming the tables via sp_rename. For example (with SET QUOTED_IDENTIFIER ON):

      CREATE PROC spCreateTable (@TableName sysname)
      AS
      BEGIN
      CREATE TABLE "_TempTableName_" (
      /* ... */
      )

      EXEC sp_rename '_TempTableName_', @TableName --rename to desired name
      END
      GO

      Does anyone know how I could use @TableName directly or indirectly in the CREATE TABLE statement?

      R Offline
      R Offline
      Rob Graham
      wrote on last edited by
      #2

      CREATE PROC spCreateTable (@TableName sysname)
      AS
      BEGIN
      declare @Tb NVarchar(80)
      select @tb = N'[dbo].[' + @tb + N']'

      CREATE TABLE @Tb(
      /* ... */
      ) ON [PRIMARY]

      END
      GO

      works for me... Some ideas are so stupid that only an intellectual could have thought of them - George Orwell

      J 1 Reply Last reply
      0
      • R Rob Graham

        CREATE PROC spCreateTable (@TableName sysname)
        AS
        BEGIN
        declare @Tb NVarchar(80)
        select @tb = N'[dbo].[' + @tb + N']'

        CREATE TABLE @Tb(
        /* ... */
        ) ON [PRIMARY]

        END
        GO

        works for me... Some ideas are so stupid that only an intellectual could have thought of them - George Orwell

        J Offline
        J Offline
        Jeff J
        wrote on last edited by
        #3

        That was what I originally did, but I keep getting invalid identifier errors with SQL Server 2K. I was hoping this was just something specific to T-SQL, as opposed to the syntaxes I am more familiar with. There must be a switch I'm incorrectly setting somewhere, since your script files parse correctly. Thanks for confirming my original approach is not what I need to change. Cheers

        R 1 Reply Last reply
        0
        • J Jeff J

          That was what I originally did, but I keep getting invalid identifier errors with SQL Server 2K. I was hoping this was just something specific to T-SQL, as opposed to the syntaxes I am more familiar with. There must be a switch I'm incorrectly setting somewhere, since your script files parse correctly. Thanks for confirming my original approach is not what I need to change. Cheers

          R Offline
          R Offline
          Rob Graham
          wrote on last edited by
          #4

          Hmmm.. This works fine with SQL 2k for me, However I usually don't change the Quoted Identifiers from the default (which I believe is off...). I always rebuild the table name into an NVARCHR local variable and delimit with the []. I think whwn we went from SQL7 to 2k the table owner part also became necessary [dbo]. The invalid identifiesr error suggests either white space or illegal characters in the name... Some ideas are so stupid that only an intellectual could have thought of them - George Orwell

          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