How to Rename a Table in SQL Server

Galaxy Glossary

How do I rename a table in SQL Server without losing data?

Use sp_rename to change a table’s name in SQL Server without affecting its data or constraints.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

Why might you need to rename a SQL Server table?

Renaming clarifies business meaning, aligns with new naming conventions, or frees a name for a replacement table. It keeps data intact and avoids the overhead of creating a new table.

What is the exact syntax for renaming a table?

The native system stored procedure sp_rename accepts the current fully qualified table name and the desired new name. Optionally specify the object type.

Syntax reference

EXEC sp_rename
@objname = 'schema.current_table_name',
@newname = 'new_table_name',
@objtype = 'OBJECT'; -- optional but recommended

How do you rename an ecommerce table safely?

Wrap the command in a transaction, clear dependent cached plans, and update documentation. Example shows renaming Orders to CustomerOrders in the dbo schema.

Step-by-step example

BEGIN TRAN;

-- 1. Rename table
EXEC sp_rename 'dbo.Orders', 'CustomerOrders', 'OBJECT';

-- 2. Refresh dependent views or procedures if needed
EXEC sp_refreshsqlmodule 'dbo.GetRecentOrders';

COMMIT;

What precautions should you take before renaming?

Create a full backup, search codebase for hard-coded table names, update ORM mappings, and notify teams. Always run tests in staging first.

Which common mistakes should you avoid?

Do not omit the schema prefix—SQL Server may look in dbo by default and miss the intended table. Also, avoid renaming during high-traffic windows to prevent plan cache churn.

Can you roll back a rename?

Yes. Run sp_rename again with the old name or execute ROLLBACK if you are still inside the same transaction.

What are the side effects on indexes, triggers, and foreign keys?

The table’s internal object_id stays the same, so indexes, triggers, and constraints remain intact. Only the metadata name changes.

How do you rename a table and retain schema qualifiers?

sp_rename cannot move a table between schemas. To change schema, use ALTER SCHEMA target_schema TRANSFER source_schema.table_name; after (or instead of) renaming.

Why How to Rename a Table in SQL Server is important

How to Rename a Table in SQL Server Example Usage


-- Rename Orders to CustomerOrders in ecommerce database
BEGIN TRAN;
EXEC sp_rename 'dbo.Orders', 'CustomerOrders', 'OBJECT';
COMMIT;

How to Rename a Table in SQL Server Syntax


EXEC sp_rename @objname = 'schema.current_table_name',
               @newname = 'new_table_name',
               @objtype = 'OBJECT';
-- Example in ecommerce context
EXEC sp_rename @objname = 'dbo.Customers',
               @newname = 'ShopCustomers',
               @objtype = 'OBJECT';

Common Mistakes

Frequently Asked Questions (FAQs)

Does sp_rename update foreign key references automatically?

Yes. References use internal object IDs, so no manual change is needed.

Can I undo a rename after the transaction is committed?

You can run sp_rename again or restore from backup, but there is no single "undo" button once committed.

Is renaming logged for auditing?

Yes. The action appears in the default trace and can be captured by extended events or a DDL trigger.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo