IMG-LOGO

How to rename a table in TSQL?

andy - 09 Dec, 2020 744 Views 0 Comment

If you want to rename a table in TSQL. We can use the built-in function called sp_rename. This function will accept two parameters string. The first one will be the table name that you want to rename. The second parameter string will be the new table name. In order to be able to rename the table. You are required to be the owner of the database.

Let's say we have a table named Users and we want to rename it to NewUsers.


EXEC sp_rename 'Users', 'NewUsers'

It is normal to get the following warning message "Caution: Changing any part of an object name could break scripts and stored procedures.

What this one means is if you have any other script or stored procedure that references back to the Users table. You are required to modify the reference name manually. The replacement will not be done automatically against any table reference used in scripts. 

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles

How to restore database using SQL query in TSQL?

If you have a database backup bak file extension and want to restore it using SQL query You can use the built in RESTORE DATABASE function Remember in order to be able to restore a database successfully You need to ...

How to get all table sizes in TSQL?

To get the information about how much space or size used by tables You can retrieve the size information by linking multiple tables in sys tables There are two tables that hold this information The first one is the sys ...