SQL Server to modify column names and table names _mssql

Source: Internet
Author: User
The code is as follows:
EXEC sp_rename ' table name. [Original column name] ', ' new column name ', ' column '

Transact-SQL Reference
sp_rename
Changes 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 ']

Parameters
[@objname =] ' object_name '
is the current name of a User object (table, view, column, stored procedure, trigger, default value, database, object, or rule) or data type. If the object you want to rename is a column in the table, then object_name must be a table.column form. If the index is to be renamed, then the object_name must be a table.index form. object_name is a nvarchar (776) type with no default value.
[@newname =] ' New_name '
is the new name of the specified object. New_name must be part of the name and follow the rules for identifiers. NewName is a sysname type with no default value.
[@objtype =] ' Object_type '
Is the type of object to rename. Object_type is a varchar (13) type with a default value of NULL and the following values are desirable.
Value Description
Column to rename.
Database user-defined databases. This option is required to rename the database.
Index user-defined indexes.
OBJECT is the type of item that is tracked in sysobjects. For example, object can be used to rename constraints (CHECK, FOREIGN key, Primary/unique key), user tables, views, stored procedures, triggers, and rules.
Userdatatype the user-defined data type that is added by executing the sp_addtype.
Return code value
0 (successful) or not 0 digits (failed)
Comments
You can change only the object name or data type name in the current database. The names of most system data types and system objects cannot be changed.
When you rename a view, information about the view in the sysobjects table is updated. When you rename a stored procedure, information about the procedure in the sysobjects table is updated.
Whenever you rename a PRIMARY KEY or a UNIQUE constraint, sp_rename renames the associated index automatically. If the renamed index is associated with the PRIMARY key constraint, then sp_rename also automatically renames the primary key.
After important renaming stored procedures and views, clear the procedure cache to ensure that all related stored procedures and views are recompiled.
Both of these objects can be quickly deleted and rebuilt because neither the stored procedure nor the view stores the data. To get the best results when renaming a text object, you should delete and re-create the object with its new name.
Permissions
The sysadmin fixed server role members, db_owner, and db_ddladmin fixed database role members or object owners can perform sp_rename. Only the sysadmin and dbcreator fixed server role members can perform the sp_rename as a object_type.
Example
A. Renaming tables
The following example renames the table customers to custs.
EXEC sp_rename ' customers ', ' custs '
B. Renaming columns
The following example renames the column contact title in table customers to title.
EXEC sp_rename ' customers. [Contact title] ', ' title ', ' COLUMN '

Please see
ALTER TABLE
CREATE DEFAULT
CREATE PROCEDURE
CREATE rule
CREATE TABLE
CREATE TRIGGER
CREATE VIEW
Data type
SETUSER
Sp_addtype
Sp_depends
Sp_renamedb
System stored Procedures

Note: The System data table can not be changed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.