SQLTeam.com | Weblogs | Forums

Change a table name dynamically


I want to change table names dynamically.something like this:
Declare @NP varchar(50) = 'alCustomers'+ Left(getDate(),

EXEC sp_rename 'dbo.Customer', 'SalCustomers;
However, I do not seem to be able to do this. So how can I concatenate a Name wiih a date to dynamically change a table name.
Thank you


You will have to form a string with all the command then use exec on that string, eg:

Declare @NP varchar(50)
SET @NP = 'sp_rename ''Customer'', ''alCustomers' + getdate() + ''''


I haven't try the syntax, so make sure you have enough single quote.


Thank you
I will try it.