missing quotes?

  • hi guys can anyone tell me if they see something wrong with this line

    SET IDENTITY_INSERT ''+ @TABLENAME + ' ' ON

    INSERT INTO ' '+ @TABLENAME +' '

    it keeps telling me :

    Incorrect syntax near ''.

    :w00t:

  • You cant do that unless you use dynamic sql.

    DECLARE @sql NVARCHAR(500)

    SET @sql = N'

    SET IDENTITY_INSERT '+ @TABLENAME + ' ON

    INSERT INTO '+ @TABLENAME

    EXECUTE sp_executesql @sql

  • i have sorry i should had put the whole code

    SET NOCOUNT ON

    DECLARE @TABLENAME VARCHAR(255)

    select @TABLENAME='customers_'+ CONVERT(CHAR(4),YEAR(GETDATE()) -1)

    SET IDENTITY_INSERT '+ @TABLENAME + ' ON

    INSERT INTO '+ @TABLENAME'

    i tried changing it to single quotes and i get the same error.

  • You have to create you statement as a string and execute it via exec or sp_executesql. Look at my example above.

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

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