T-SQL rename Table Sql Server

T-SQL rename Table Sql Server

When you modify database schema on Sql Server with Microsoft SQL Server Management Studio you can simply create script with Generate Change Script function.

This is not allowed when you rename a database object, for example when you rename a table (F2 key) or rename Stored Procedure, but also when you rename a table column and so on.

In this situation you can use the system stored procedure sp_rename (Transact-SQL).

Below some utilization examples:

Rename table on database:

EXEC sp_rename 'Vecchio_Nome_Tabella', 'Nuovo_Nome_Tabella';

Rename table column on database:

EXEC sp_rename 'Nome_Tabella.Vecchio_Nome_Colonna', 'Nuovo_Nome_Colonna', 'COLUMN';

Applies to SQL Server 2005 / 2008.

1 COMMENT

comments user
girolamo

Salve,
una domanda. Cosa accade se viene fatto il rename di una tabella mentre questa รจ interrogata?
Esempio ho una

select * from table1

e allo stesso tempo eseguo

exec sp_rename table2 table1

Grazie
Girolamo

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.