Creating Dynamic Tables

  • I want to create dynamic tables, I am using the follwing script but it gives me error

    declare @tablename varchar (100)

    set @tablename = '#BackupFile_' + replace(replace(cast(getdate() as varchar),' ',''),':','')

    select @tablename

    create table @tablename

    (

    [name] [nvarchar] (100)

    )

    error :

    Incorrect syntax near '@tablename'.

  • Try this.

    declare @tablename varchar (100)

    set @tablename = '#BackupFile_' + replace(replace(cast(getdate() as varchar),' ',''),':','')

    select @tablename

    EXEC ('CREATE TABLE '+@tablename+ '([name] [nvarchar] (100))')

     

    Ram

     

     

  • I would just use the convert function, something like:

    set

    @tablename = '#BackupFile_' + convert(varchar, getdate(), 112)

    Which gives values such as, #BackupFile_20060929

Viewing 3 posts - 1 through 2 (of 2 total)

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