Add SQL table name with today's date

I understand that I can modify the sql table using the following command:

EXEC sp_rename 'customers', 'custs'

How can I add this so that the new table has a date as a suffix?

I am making variations on the topic below with little success.

EXEC sp_rename 'customers', 'customers +(CONVERT(VARCHAR(8),GETDATE(),3))'

Any help is greatly appreciated.

+3
source share
4 answers

That sounds really bad! you have to evaluate your design, renaming tables with dates in names, says that you will create many tables, each for a different date. You could add a date column to your table and use this to distinguish data, rather than create completely new tables for different dates.

SQL Server. "" .

, :

DECLARE @Value varchar(500)
SET @Value='customers' +(CONVERT(VARCHAR(8),GETDATE(),3))
EXEC sp_rename 'customers', @Value
+5

T-SQL, ? . - :

EXEC sp_rename 'customers', 'customers' +(CONVERT(VARCHAR(8),GETDATE(),3))
+2
DECLARE @TableName varchar(50)

SELECT @TableName = (SELECT 'Customers_' + convert(varchar(50),GetDate(),112))

EXEC sp_rename 'customers', @TableName
+1

, , :

EXEC sp_rename 'customers', 'customers20100408' 
0

Source: https://habr.com/ru/post/1740153/


All Articles