Dynamic SQL error

  • i tried using dynamic sql for creating a filegroup as a task in the beginning of the package but that is giving me an error now..could you plz give me some kind of direction on how i can solve this:

    declare @newfilegroup varchar(60)

    set @newfilegroup = substring(CONVERT(VARCHAR, DATEPART(YEAR, GETDATE())), 3, 4)

    EXEC ('ALTER DATABASE test ADD FILEGROUP ' + @newfilegroup)

    this gives me a synatx error where as

    declare @newfilegroup varchar(60)

    set @newfilegroup = 'a' + substring(CONVERT(VARCHAR, DATEPART(YEAR, GETDATE())), 3, 4)

    EXEC ('ALTER DATABASE test ADD FILEGROUP ' + @newfilegroup)

    works fine

  • My guess (note, guess) is that filegroup names cannot start with a number.

    -- Cory

  • filegorup names can start with number.. right now they are manually created as 07, 08 and so on..

  • declare @newfilegroup varchar(60)

    set @newfilegroup = substring(CONVERT(VARCHAR, DATEPART(YEAR, GETDATE())), 3, 4)

    EXEC ('ALTER DATABASE northwind ADD FILEGROUP [' + @newfilegroup + ']')

    It needs to be wrapped in the [ ].

    -- Cory

  • Thankyou so much!

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

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