Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Table Re Name

Author  Topic 

sanjay5219
Posting Yak Master

240 Posts

Posted - 2011-08-26 : 09:14:36
Hi All,

I am trying to execute this query.

sp_rename 'Emp','Employee' + CAST(GETDATE() as nvarchar(max))

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '+'.


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-26 : 09:30:23
first store expression in a variable and then use it

DECLARE @TableName varchar(100)

SET @TableName='Employee' + CAST(GETDATE() as nvarchar(max))

sp_rename 'Emp',@TableName


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sanjay5219
Posting Yak Master

240 Posts

Posted - 2011-08-26 : 09:33:18
getting error Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'sp_rename'.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-26 : 09:43:33
[code]
DECLARE @TableName varchar(100)

SET @TableName='Employee' + CAST(GETDATE() as nvarchar(max))

EXEC sp_rename 'Emp',@TableName
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-08-29 : 03:45:44
Why are you appending current date as part of table name? Are you creating tables dynamically?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -