sp_rename

Changes the name of a user-created object or user-defined datatype in the current database.

Syntax

sp_rename objname, newname [,“index” | “column” | “partition”]

Parameters

Examples

Usage

There are additional considerations when using sp_rename:
  • sp_rename changes the name of a user-created object or datatype. You can change only the name of an object or datatype in the database in which you issue sp_rename.

  • When you are renaming a column or index, do not specify the table name in newname. See Examples 2, 3, and 5.

  • If a column and an index have the same name, use the [,“index” | “column”] argument, which specifies whether to rename the index or the column. In the following sample, assume that both an index and a column named idx exist:

    sp_rename "t.idx", new_idx, "column"
    -------------
    Column name has been changed. (Return status = 0)
    sp_rename "t.idx", new_idx, "index"
    -------------
    Index name has been changed. (Return status = 0)
  • If you change the name of a an object or column name referenced by a view, you see a warning message, such as:
    Changing an object or column name could break
    existing stored procedures, cached statements or
    other compiled objects.
  • sp_engine can run in sessions using chained transaction mode if there are no open transactions.

  • You cannot change the names of system objects and system datatypes.

Permissions

You must be the object owner to execute sp_rename. Permission checks do not differ based on the granular permissions settings.

Use the setuser command to assume another database user’s identity to rename objects owned by other users.

Auditing

Values in event and extrainfo columns from the sysaudits table are:

InformationValues
Event

38

Audit option

exec_procedure

Command or access audited

Execution of a procedure

Information in extrainfo
  • Roles – Current active roles

  • Keywords or options – NULL

  • Previous value – NULL

  • Current value – NULL

  • Other information – All input parameters

  • Proxy information – Original login name, if set proxy in effect

Related reference
sp_depends
sp_rename