Rename column SQL Server 2008

前端 未结 12 2038
名媛妹妹
名媛妹妹 2020-11-27 08:52

I am using SQL Server 2008 and Navicat. I need to rename a column in a table using SQL.

ALTER TABLE table_name RENAME COLUMN old_name to new_name;

相关标签:
12条回答
  • 2020-11-27 09:30

    In my case, I was using MySQL WorkBench and

    ALTER TABLE table_name RENAME COLUMN old_name new_name varchar(50) not null; 
    // without TO and specify data type of that column 
    

    was enough to change the column name!

    0 讨论(0)
  • 2020-11-27 09:40

    Use sp_rename

    EXEC sp_RENAME 'TableName.OldColumnName' , 'NewColumnName', 'COLUMN'
    

    See: SQL SERVER – How to Rename a Column Name or Table Name

    Documentation: sp_rename (Transact-SQL)

    For your case it would be:

    EXEC sp_RENAME 'table_name.old_name', 'new_name', 'COLUMN'
    

    Remember to use single quotes to enclose your values.

    0 讨论(0)
  • 2020-11-27 09:43

    Alternatively to SQL, you can do this in Microsoft SQL Server Management Studio. Here are a few quick ways using the GUI:

    First Way

    Slow double-click on the column. The column name will become an editable text box.


    Second Way

    Right click on column and choose Rename from the context menu.

    For example:

    To Rename column name


    Third Way

    This way is preferable for when you need to rename multiple columns in one go.

    1. Right-click on the table that contains the column that needs renaming.
    2. Click Design.
    3. In the table design panel, click and edit the textbox of the column name you want to alter.

    For example: MSSMS Table Design Example

    NOTE: I know OP specifically asked for SQL solution, thought this might help others :)

    0 讨论(0)
  • 2020-11-27 09:43

    It would be a good suggestion to use an already built-in function but another way around is to:

    1. Create a new column with same data type and NEW NAME.
    2. Run an UPDATE/INSERT statement to copy all the data into new column.
    3. Drop the old column.

    The benefit behind using the sp_rename is that it takes care of all the relations associated with it.

    From the documentation:

    sp_rename automatically renames the associated index whenever a PRIMARY KEY or UNIQUE constraint is renamed. If a renamed index is tied to a PRIMARY KEY constraint, the PRIMARY KEY constraint is also automatically renamed by sp_rename. sp_rename can be used to rename primary and secondary XML indexes.

    0 讨论(0)
  • 2020-11-27 09:44

    Try:

    EXEC sp_rename 'TableName.OldName', 'NewName', 'COLUMN'
    
    0 讨论(0)
  • 2020-11-27 09:45

    Sql Server management studio has some system defined Stored Procedures(SP)
    One of which is used to rename a column.The SP is sp_rename

    Syntax: sp_rename '[table_name].old_column_name', 'new_column_name'
    For further help refer this article: sp_rename by Microsoft Docs

    Note: On execution of this SP the sql server will give you a caution message as 'Caution: Changing any part of an object name could break scripts and stored procedures'.This is critical only if you have written your own sp which involves the column in the table you are about to change.

    0 讨论(0)
提交回复
热议问题