Join Us on : WhatsApp | Mobile app

Monday, June 4, 2018

How to Rename a Column Name or Table Name using SQL Script In MS-SQL?

SP_RANAME: is a system function is used to rename column or any database objects (table or view or storedprocedure or function) 
The script for renaming any column or index or datatype:
EXECUTE SP_Rename 'Tablename.colname', 'Newcolname','COLUMN'
The script for renaming any object (table, view, usp etc):
EXECUTE SP_Rename 'Tablename' , 'New_name'
The above two examples of renaming database object.
·        Renaming database table column to new name.
·        Renaming database table to new name.
In both the cases we will first see existing table. Rename the object. Test object again with new name.
Syntax
sp_rename 'object_name' ,'new_name','object_type'
Permissions
To rename objects, columns, and indexes, requires ALTER permission on the object. To rename user types, requires CONTROL permission on the type. To rename a database, requires membership in the sysadmin or dbcreator fixed server roles.


No comments:

Post a Comment