Database design is the organization of data. A good database design makes it successful. Its recommended to follow a good pattern for the table names and their columns. In some cases, you may be required to rename a column name of the table to maintain a proper architecture and naming pattern. The sp_rename stored procedure is used to rename a column name in a table in MSSQL server database.
Syntax:
The syntax of the sp_rename stored procedure is:
sp_rename 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'
Rename a Column in SQL Server
For example, I have a table named Accounts in the MSSQL server database. This table contains a column named “cust_id”. For some reason, we need to rename this column to “customer_id”. See below screenshot:
Open query windows and execute the following query. Here Test_db
is the database name.
USE Test_db GO sp_rename 'Accounts.cust_id', 'customer_id' , 'COLUMN' GO
This may show you a warning message with the result, But this will rename the column name successfully. Checked the table structure again, and I found that the column is updated as required.
Conclusion
This tutorial helped you to rename a table column name in the SQL Server database.