How to rename table with the result of a query?

  • Hi, does anyone knows how to change the name of a table mixing characters with the result of a query.

    Example:

    DECLARE @DATE AS VARCHAR (8)

    SET SELECT @DATE = CAST(convert(varchar, getdate(), 112)AS VARCHAR(8))

    SP_RENAME 'ATT_', 'ATT_' + @DATE

    of course this doesn't work but does anyone know how to do it?

    Thanks for the help!

    FJM

  • Your code seems to work fine by just adding another variable.

    DECLARE @newname VARCHAR(50)

    DECLARE @DATE AS VARCHAR (8)

    SET @DATE = CAST(convert(varchar, getdate(), 112)AS VARCHAR(8))

    SET @newname = 'ATT_' + @DATE

    EXEC SP_RENAME 'ATT_', @newname

  • Lol.... it's so easy!!!

  • Thanks!!!

  • Hi,

    DECLARE @tbl SYSNAME, @tbl_new SYSNAME

    SELECT @tbl = 'ATT_',

    @tbl_new = @tbl + CAST(convert(varchar, getdate(), 112)AS VARCHAR(8))

    EXEC sp_rename @tbl, @tbl_new, 'OBJECT'

    [font="Verdana"]CU
    tosc[/font]

    www.insidesql.org

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

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