In Microsoft SQL Server, you can rename a table using the sp_rename
system Stored Procedure.
Syntax:
EXEC sp_rename 'schema_name.old_table_name', 'new_table_name';
Steps:
- Specify the Schema and Old Table Name:
- Include the schema name if the table is not in the default schema (e.g.,
dbo.old_table_name
).
- Include the schema name if the table is not in the default schema (e.g.,
- Specify the New Table Name:
- Provide the new name without changing the schema.
Example:
Rename a table employees
to staff
in the dbo
schema:
EXEC sp_rename 'dbo.employees', 'staff';
Notes:
- Renaming a table does not automatically update references to it (e.g., in views, stored procedures, or foreign keys). These must be updated manually.
- Avoid using
sp_rename
in critical or large production environments without testing, as it might affect dependencies.