相關的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.