To rename a column
in SQL Server, you can use the sp_rename
stored procedure.
The procedure takes two arguments: the name of the existing column and the new name for the column.
Please note that renaming a column will also update all references to that column in triggers, views, stored procedures, and functions.
RENAME Column syntax
The syntax to rename a column from a table in the SQL Server database is as follows:
EXEC sp_rename 'Table.Old_Column', 'New_Column', 'COLUMN'
RENAME Column example
Employee table:
Column name | Data_type |
---|---|
id | int |
name | varchar(250) |
dep_id | int |
address | varchar(400) |
EXEC sp_rename 'employee.address', 'new_address', 'COLUMN' ;
This will rename the "address" column in the "employee" table to "new_address".
Result
Column name | Data_type |
---|---|
id | int |
name | varchar(250) |
dep_id | int |
new_address | varchar(400) |