OBJECTPROPERTY and sp_rename

Source: Internet
Author: User
Errors are inevitable during development. To correct errors and normalize them. You can use the system stored procedures sp_rename and OBJECTPROPERTY of MSSQLServer to change the rename.

Errors are inevitable during development. To correct errors and normalize them. You can use the system stored procedures sp_rename and OBJECTPROPERTY of ms SQL Server to change the rename.

For more information about OBJECTPROPERTY, see:

Errors are inevitable during development. To correct errors and normalize them. You can use the system stored procedures sp_rename and OBJECTPROPERTY of ms SQL Server to change the rename.

Instance 1: Change the table name:
The Code is as follows:
If exists (select top 1 FROM dbo. sysobjects WHERE id = object_id (n' [dbo]. [Employee] ')
And objectproperty (id, N 'isusertable') = 1
)
EXECUTE sp_rename 'employe', 'member'



Instance 2: Change the field name:

The Code is as follows:
EXECUTE sp_rename 'member. [EmployeeName] ', 'name', 'column'



Instance 3: Change the primary key:

The Code is as follows:
If exists (select top 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'



Instance 4: Change the stored procedure name:

The Code is as follows:
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 ', 'memberinss'



After successful execution, you will receive a message:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.