SQL Server Tutorialprovides basic and advanced concepts of C# for beginners and professionals.

SQL Rename Table

Back to: SQL Server Tutorial

Renaming a table in Microsoft SQL Server can be done using the sp_rename stored procedure. Here’s the syntax you would use:

EXEC sp_rename 'old_table_name', 'new_table_name';

Here's a step-by-step example:

  1. Identify the Old and New Table Names:

    • Suppose you have a table named Employees and you want to rename it to Staff.
  2. Execute the Rename Command:

    EXEC sp_rename 'Employees', 'Staff';

Important Notes:

  • Schema Prefix: If your table is in a specific schema, you should include the schema name in the table name, like 'schema_name.old_table_name'. For example:

    EXEC sp_rename 'dbo.Employees', 'Staff';
  • Impact on Dependencies: Renaming a table can affect stored procedures, views, or functions that depend on the table. Make sure to check and update any dependent objects.

  • Permissions: You need appropriate permissions to rename a table. Typically, you need ALTER permission on the table or membership in the db_owner role.

  • Error Handling: If you attempt to rename a table to a name that already exists, you will encounter an error. Ensure the new table name is unique within the schema.

Always remember to test such changes in a development or staging environment before applying them to production.

Scroll to Top