How to rename a table or column using T-SQL in Microsoft SQL
2011-02-05, 7:30 AM
So yeah, you could use the Microsoft SQL Server Management Studio UI
to rename your table or column. But sometimes you need to do the rename
in T-SQL. Here’s how.
Changes the name of a user-created object
in the current database. This object can be a table, index, column,
alias data type, or Microsoft .NET Framework common language runtime
(CLR) user-defined type.
Caution
Changing
any part of an object name can break scripts and stored procedures. We
recommend you do not use this statement to rename stored procedures,
triggers, user-defined functions, or views; instead, drop the object and
re-create it with the new name.
Is the current qualified or nonqualified name of the user object
or data type. If the object to be renamed is a column in a table, object_name must be in the form table.column or schema.table.column. If the object to be renamed is an index, object_name must be in the form table.index or schema.table.index.
Quotation marks are only necessary if a qualified object is
specified. If a fully qualified name, including a database name, is
provided, the database name must be the name of the current database. object_name is nvarchar(776), with no default.
[ @newname = ] 'new_name'
Is the new name for the specified object. new_name must be a one-part name and must follow the rules for identifiers. newname is sysname, with no default.
Note
Trigger names cannot start with # or ##.
[ @objtype = ] 'object_type'
Is the type of object being renamed. object_type is varchar(13), with a default of NULL, and can be one of these values.
Value
Description
COLUMN
A column to be renamed.
DATABASE
A user-defined database. This object type is required when renaming a database.
INDEX
A user-defined index.
OBJECT
An item of a type tracked in sys.objects.
For example, OBJECT could be used to rename objects including
constraints (CHECK, FOREIGN KEY, PRIMARY/UNIQUE KEY), user tables, and
rules.
USERDATATYPE
An alias data type or CLR User-defined Types added by executing CREATE TYPE or sp_addtype.
You can change the name of an
object or data type in the current database only. The names of most
system data types and system objects cannot be changed.
sp_rename automatically renames
the associated index whenever a PRIMARY KEY or UNIQUE constraint is
renamed. If a renamed index is tied to a PRIMARY KEY constraint, the
PRIMARY KEY constraint is also automatically renamed by sp_rename.
sp_rename can be used to rename primary and secondary XML indexes.
Renaming a stored procedure,
function, view, or trigger will not change the name of the corresponding
object name in the definition column of the sys.sql_modules
catalog view. Therefore, we recommend that sp_rename not be used to
rename these object types. Instead, drop and re-create the object with
its new name.
Renaming an object such as a
table or column will not automatically rename references to that object.
You must modify any objects that reference the renamed object manually.
For example, if you rename a table column and that column is referenced
in a trigger, you must modify the trigger to reflect the new column
name. Use sys.sql_expression_dependencies to list dependencies on the object before renaming it.
To rename objects, columns, and
indexes, requires ALTER permission on the object. To rename user types,
requires CONTROL permission on the type. To rename a database, requires
membership in the sysadmin or dbcreator fixed server roles