[Switch] sp_rename usage in SQL SERVER

To change the column names of the table due to changes in demand, we usually go to the Enterprise Manager to select the server -> database -> table, and then modify the table, which is too troublesome. 

EXEC sp_rename 'table name.[original column name]', 'new column name', 'column'

 
Transact-SQL Reference 

sp_rename -- Change the name of a user-created object (such as a table, column, or user-defined data type) in the current database.


grammar 

sp_rename [ @objname = ] 'object_name' , [ @newname = ] 'new_name' [ , [ @objtype = ] 'object_type' ]

parameter 

[@objname =] 'object_name'

is the current name of a user object (table, view, column, stored procedure, trigger, default, database, object, or rule) or data type. If the object to be renamed is a column in a table, then object_name must be of the form table.column. If an index is to be renamed, then object_name must be of the form table.index. object_name is of type nvarchar(776) with no default value. 

[@newname =] 'new_name'

is the new name of the specified object. new_name must be part of the name and follows the rules for identifiers. newname is of type sysname with no default value. 

[@objtype =] 'object_type'

is the type of object to be renamed. object_type is varchar(13) type, its default value is NULL, and it can take the following values. 

Value description 

COLUMN The column to be renamed.
DATABASE User-defined database. This option is required to rename the database.
INDEX User-defined index.
OBJECT An item of the type tracked in sysobjects. For example, OBJECT can be used to rename objects such as constraints (CHECK, FOREIGN KEY, PRIMARY/UNIQUE KEY), user tables, views, stored procedures, triggers, and rules.
USERDATATYPE User-defined data type added by executing sp_addtype.

return code value 

0 (success) or a non-zero number (failure)


Notes 

Only object names or datatype names in the current database can be changed. The names of most system data types and system objects cannot be changed.

When a view is renamed, the information about the view in the sysobjects table is updated. When a stored procedure is renamed, the information about the procedure in the sysobjects table is updated.

Whenever a PRIMARY KEY or UNIQUE constraint is renamed, sp_rename automatically renames the associated index. If the renamed index is associated with a PRIMARY KEY constraint, sp_rename also automatically renames the primary key.

important 

After renaming stored procedures and views, flush the procedure cache to ensure that all related stored procedures and views are recompiled. 
Since neither stored procedures nor views store data, both objects can be quickly deleted and rebuilt. For best results when renaming text objects, delete and recreate the object with its new name. 

permission 

Members of the sysadmin fixed server role, members of the db_owner and db_ddladmin fixed database roles, or object owners can execute sp_rename. Only members of the sysadmin and dbcreator fixed server roles can execute sp_rename with "database" as object_type.

Example 
A. Renaming a table 

The following example renames the table customers to custs.

EXEC sp_rename 'customers', 'custs'

B. Rename the column 

The following example renames the column contact title in the table customers to title.

EXEC sp_rename 'customers.[contact title]', 'title', 'COLUMN'

See 

copy code
ALTER TABLE

CREATE DEFAULT

CREATE PROCEDURE

CREATE RULE

CREATE TABLE

CREATE TRIGGER

CREATE VIEW

type of data

SETUSER

sp_addtype

sp_depends

sp_renamedb

system stored procedure
copy code

Reprinted from: http://www.cnblogs.com/no7dw/archive/2010/03/04/1678287.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325295547&siteId=291194637
Recommended