Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Databases»SQL Server»How to Rename a Column in SQL Server

    How to Rename a Column in SQL Server

    RahulBy RahulJune 16, 20192 Mins ReadUpdated:June 28, 2021

    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'
    
    Caution: Changing any part of an object name could break scripts and stored procedures. Make sure to update all the required scripts and stored procedures with updated names.

    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_dbis 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.

    Rename table column in SQL Server

    Conclusion

    This tutorial helped you to rename a table column name in the SQL Server database.

    mssql SQL SErver stored procedure
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install Apache 2.4 & PHP 7.4 on Amazon Linux
    Next Article How to Count Number of Lines in File with Python

    Related Posts

    How to Backup SQL Server Database

    Updated:June 28, 20212 Mins Read

    How To Restore SQL Server Database

    Updated:July 12, 20213 Mins Read

    How to Rename a SQL Server Database

    Updated:June 21, 20213 Mins Read

    (Fixed) Cannot drop the database because it is being used for replication. (Microsoft SQL Server, Error: 3724)

    Updated:June 23, 20211 Min Read

    How to Find Currently Running Query in SQL Server

    Updated:May 11, 20171 Min Read

    FIXED MSSQL Error 3154 – The backup set holds a backup of a database other than the existing database

    Updated:October 15, 20161 Min Read

    Leave A Reply Cancel Reply

    Recent Posts
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.