Creating Table with GetDate Function

  • Is there a way to create a table with the current date as part of the table name? I tried using a create table statement using the getdate function but it didn't like that. Here's what I tried:

    create table tblTEST+'_'+getdate()

    The result was: Incorrect syntax near '+'.

    Is there a way to create a table with the date as part of the table name?

    Thanks,

    Brian

  • try this

    declare @tbname varchar(60)

    set @tbname = 'tblTEST'+'_'+convert(varchar,getdate(),112)

    select @tbname

    exec('create table '+@tbname+'(c1 int)')

  • CREATE TABLE [AA] (

     [SentDate] [smalldatetime] NULL CONSTRAINT [DF_ADS_RemitanceTransaction_SentDate] DEFAULT (getdate()))




    My Blog: http://dineshasanka.spaces.live.com/

  • http://www.sommarskog.se/dynamic_sql.html explains how you can do it, and also, why this is a bad idea in most cases.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • You do not need dynamic sql to do this. You can crate the table with a dummy name first, and use sp_rename to give it the name you want :

    declare @tblname sysname

    set @tblname = 'dbo.tblTEST_' + convert(varchar(8),getdate(),112)

    print @tblname

    create table dbo.__temporaryname__ ( i int not null )

    exec sp_rename 'dbo.__temporaryname__',@tblname

Viewing 5 posts - 1 through 4 (of 4 total)

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