dynamically creating a tablename in tsql

  • hi, i'm trying to dynamically create a tablename in tsql so that the tablename contains the month and year. I keep getting an error that the stored procedure cannot be executed. can someone please tell me where i'm going wrong?

    thanks!

    declare @strsqlcreatetable as nvarchar(255)

    declare @tablename as nvarchar(255)

    declare @strsql as nvarchar(255)

    select @tablename = 'lclifecycle' + convert(nvarchar(2), datepart(mm, getdate())) + convert(nvarchar(4), datepart(yyyy, getdate()))

    print @tablename

    select @strsqlcreatetable = 'create table ' + @tablename + '(emp int)'

    print @strsqlcreatetable

    exec @strsqlcreatetable

  • declare @strsqlcreatetable as nvarchar(255)

    declare @tablename as nvarchar(255)

    declare @strsql as nvarchar(255)

    select @tablename = 'lclifecycle' + convert(nvarchar(2), datepart(mm, getdate())) + convert(nvarchar(4), datepart(yyyy, getdate()))

    print @tablename

    select @strsqlcreatetable = 'create table ' + @tablename + '(emp int)'

    print @strsqlcreatetable

    exec (@strsqlcreatetable) --there is your missing parenthesis!

    😎 :hehe:

    ============================================================
    SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
    http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]

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

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