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';
sp_rename的更多相关文章
- sqlce中不支持sp_rename修改表名
The sp_rename procedure is not avialable in SQL CE! In Sql Server 2005 Management Studio you have to ...
- SQL Server使用sp_rename重命名约束注意事项
在SQL Server中,我们可以使用sp_name这个系统存储过程重命名数据库中对象的名称. 此对象可以是表. 索引. 列. 别名,约束等数据类型(具体可以参考官方文档).上周在使用这个函数重构数据 ...
- 【转】SQL SERVER 中 sp_rename 用法
因需求变更要改表的列名,平常都是跑到Enterprise manager中选取服务器->数据库->表,然后修改表,这样太麻烦了,查了一下,可以用script搞定,代码如下: EXEC sp ...
- sql 修改列名及表名 sp_rename
因需求变更要改表的列名,平常都是跑到Enterprise manager中选取服务器->数据库->表,然后修改表,这样太麻烦了,查了一下,可以用script搞定, 代码如下: EXEC s ...
- SQL Server ->> 存储过程sp_rename重命名数据对象
1) 表转移Schema和重命名表 ALTER SCHEMA Stage TRANSFER dbo.Stage_AAA; EXEC sp_rename 'Stage.Stage_AAA', 'AAA' ...
- sp_rename sqlserver 表 列 索引 类型重命名
--[语法] sp_rename [ @objname = ] 'object_name' , [ @newname = ] 'new_name' [ , [ @objtype = ] 'object ...
- SQL SERVER 中 sp_rename 用法
转自:http://www.cnblogs.com/no7dw/archive/2010/03/04/1678287.html 因需求变更要改表的列名,平常都是跑到Enterprise manager ...
- 我的MYSQL学习心得(一) 简单语法
我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...
- SQL Server 致程序员(容易忽略的错误)
标签:SQL SERVER/MSSQL/DBA/T-SQL好习惯/数据库/需要注意的地方/程序员/容易犯的错误/遇到的问题 概述 因为每天需要审核程序员发布的SQL语句,所以收集了一些程序员的一些常见 ...
随机推荐
- startInstrumentation asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
由于手头上一直没有android level 17及以上版本的手机,有一个shell命令启动脚本的BUG,发生在SDK level 17及以上 API>=17中加入了INTERACT_ACROS ...
- tyvj100题留念
全是水题萌萌哒~0~... 留个纪念
- Swift Internal Parameter and External Parameter 外部参数和内部参数
今天跟大神又学习了些关于IOS开发Swift语言的外部参数和内部参数 func doSomething(num1: Int, num2: Int) -> Int { return num1 + ...
- 《GK101任意波发生器》升级固件发布(版本:1.0.2build851)
一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build851 编译日期:2015-06-26 ====================================== 二. ...
- JAVA WEB 的JSP(9*9乘法表+*型金字塔)
运行环境及工具: (Tomcat7) + (JAVA JDK)+ (Eclipse for J2EE) 输出9*9乘法表 代码片段的练习 增加一些简单的JS功能 <%@ page import= ...
- Frenetic Python实验(二)
实验3 packet_in_out 目的:模拟一个普通的双端口中继器. This application implements a very simple 2 port repeater where ...
- 正则匹配中文.PHP不兼容的问题
不使用: ^[\u4e00-\u9fa5_a-zA-Z0-9_]+$ 有可能兼容有问题 if(!preg_match_all("/^[\\x7f-\\xff_a-zA-Z0-9]+$/&qu ...
- PHP常用正则表达式汇总
1. 平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: 2. "^\d+$" //非负整数(正整数 + 0) 3. "^[0 ...
- php字符串常用函数
addslashes print addslahes ('She said, "Great!"'); #output #She said, \"Great!\ echo ...
- LR中HTTP协议录制模式选择
在LR中使用HTML/HTTP协议进行脚本录制时面临正确选择HTTP-based script / URL-base script 录制模式的问题,以下是比较官方的建议:1)基于浏览器的应用程序推荐使 ...