OBJECTPROPERTY与sp_rename更改对象名称的介绍

(编辑:jimmy 日期: 2024/12/23 浏览:2)

相关的OBJECTPROPERTY可参考:http://msdn.microsoft.com/en-us/library/ms176105.aspx

开发过程中,错误免不了。为了纠正错误与规范化。可以使用MS SQL Server的系统存储过程sp_rename与OBJECTPROPERTY来更改重命名。

实例一,更改表名:
复制代码 代码如下:
IF EXISTS(SELECT TOP 1 1 FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[Employee]')
AND OBJECTPROPERTY(id, N'IsUserTable') = 1
)
EXECUTE sp_rename 'Employee', 'Member'



实例二,更改字段名:

复制代码 代码如下:
EXECUTE sp_rename 'Member.[EmployeeName]', 'Name', 'COLUMN'



实例三,更改主键:

复制代码 代码如下:
IF EXISTS(SELECT TOP 1 1 FROM sysobjects WHERE id = OBJECT_ID(N'[PK__Employee__B00879C509746778]')
AND OBJECTPROPERTY(id, N'IsPrimaryKey') = 1)
EXECUTE sp_rename 'PK__Employee__B00879C509746778', 'PK_Member', 'OBJECT'



实例四,更改存储过程名:

复制代码 代码如下:
IF EXISTS(SELECT TOP 1 1 FROM sysobjects WHERE id = OBJECT_ID(N'[usp_Employee_Import]')
AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
EXECUTE sp_rename 'usp_Employee_Import', 'MemberInsSp'



执行成功之后,你会收到一条信息:

Caution: Changing any part of an object name could break scripts and stored procedures.