SQLTeam.com | Weblogs | Forums

Change a table name dynamically


#1

Hi,
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
itm


#2

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() + ''''

EXEC (@NP)

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


#3

Thank you
I will try it.