sp_rename

sp_rename

在当前数据库中更改用户创建对象的名称。 此对象可以是表、索引、列、别名数据类型或 Microsoft .NET Framework 公共语言运行时 (CLR) 用户定义类型。

更改对象名的任一部分都可能破坏脚本和存储过程。 我们建议您不要使用此语句来重命名存储过程、触发器、用户定义函数或视图;而是删除该对象,然后使用新名称重新创建该对象。

适用范围:SQL Server(SQL Server 2008 至当前版本),Windows Azure SQL Database(初始版本至当前版本)。

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

A.重命名表

下面的示例将 Sales 架构中的 SalesTerritory 表重命名为 SalesTerr。

 
 
USE AdventureWorks2012;
GO
EXEC sp_rename 'Sales.SalesTerritory', 'SalesTerr';
GO

B.重命名列

下面的示例将 SalesTerritory 表中的 TerritoryID 列重命名为 TerrID。

 
 
USE AdventureWorks2012;
GO
EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';
GO

C.重命名索引

下面的示例将 IX_ProductVendor_VendorID 索引重命名为 IX_VendorID。

 
 
USE AdventureWorks2012;
GO
EXEC sp_rename N'Purchasing.ProductVendor.IX_ProductVendor_VendorID', N'IX_VendorID', N'INDEX';
GO

D.重命名别名数据类型

下面的示例将 Phone 别名数据类型重命名为 Telephone。

 
 
USE AdventureWorks2012;
GO
EXEC sp_rename N'Phone', N'Telephone', N'USERDATATYPE';
GO

E.重命名约束

下面的示例重命名 PRIMARY KEY 约束、CHECK 约束和 FOREIGN KEY 约束。 重命名约束时,必须指定约束所属的架构。

 
 
USE AdventureWorks2012; 
GO
-- Return the current Primary Key, Foreign Key and Check constraints for the Employee table.
SELECT name, SCHEMA_NAME(schema_id) AS schema_name, type_desc
FROM sys.objects
WHERE parent_object_id = (OBJECT_ID('HumanResources.Employee')) 
AND type IN ('C','F', 'PK'); 
GO

-- Rename the primary key constraint.
sp_rename 'HumanResources.PK_Employee_BusinessEntityID', 'PK_EmployeeID';
GO

-- Rename a check constraint.
sp_rename 'HumanResources.CK_Employee_BirthDate', 'CK_BirthDate';
GO

-- Rename a foreign key constraint.
sp_rename 'HumanResources.FK_Employee_Person_BusinessEntityID', 'FK_EmployeeID';

-- Return the current Primary Key, Foreign Key and Check constraints for the Employee table.
SELECT name, SCHEMA_NAME(schema_id) AS schema_name, type_desc
FROM sys.objects
WHERE parent_object_id = (OBJECT_ID('HumanResources.Employee')) 
AND type IN ('C','F', 'PK'); 

name                                  schema_name        type_desc
------------------------------------- ------------------ ----------------------
FK_Employee_Person_BusinessEntityID   HumanResources     FOREIGN_KEY_CONSTRAINT
PK_Employee_BusinessEntityID          HumanResources     PRIMARY_KEY_CONSTRAINT
CK_Employee_BirthDate                 HumanResources     CHECK_CONSTRAINT
CK_Employee_MaritalStatus             HumanResources     CHECK_CONSTRAINT
CK_Employee_HireDate                  HumanResources     CHECK_CONSTRAINT
CK_Employee_Gender                    HumanResources     CHECK_CONSTRAINT
CK_Employee_VacationHours             HumanResources     CHECK_CONSTRAINT
CK_Employee_SickLeaveHours            HumanResources     CHECK_CONSTRAINT

(7 row(s) affected)

name                                  schema_name        type_desc
------------------------------------- ------------------ ----------------------
FK_Employee_ID                        HumanResources     FOREIGN_KEY_CONSTRAINT
PK_Employee_ID                        HumanResources     PRIMARY_KEY_CONSTRAINT
CK_BirthDate                          HumanResources     CHECK_CONSTRAINT
CK_Employee_MaritalStatus             HumanResources     CHECK_CONSTRAINT
CK_Employee_HireDate                  HumanResources     CHECK_CONSTRAINT
CK_Employee_Gender                    HumanResources     CHECK_CONSTRAINT
CK_Employee_VacationHours             HumanResources     CHECK_CONSTRAINT
CK_Employee_SickLeaveHours            HumanResources     CHECK_CONSTRAINT

(7 row(s) affected)

F.重命名统计信息

下面的示例创建一个名为 contactMail1 的统计信息对象,然后使用 sp_rename 将统计信息重命名为 NewContact。重命名统计信息时,必须采用格式 schema.table.statistics_name 指定该对象。

 

适用范围:SQL Server(SQL Server 2012 到 SQL Server 2014、Windows Azure SQL Database)。

 
 
CREATE STATISTICS ContactMail1
    ON Person.Person (BusinessEntityID, EmailPromotion)
    WITH SAMPLE 5 PERCENT;

sp_rename 'Person.Person.ContactMail1', 'NewContact','Statistics';
posted @ 2015-09-14 10:58  赵孟蒙  Views(965)  Comments(0Edit  收藏  举报