796

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;

This statement doesn't work.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Serhio g. Lazin
  • 9,442
  • 6
  • 25
  • 33
  • 2
    check out http://stackoverflow.com/questions/174582/how-do-i-rename-a-column-in-a-database-table-using-sql – swetha Apr 30 '13 at 09:36
  • 6
    Note that this is not quite a duplicate of question #174582 which swetha linked: this one is MS SQL-specific, that one is database-agnostic. –  Oct 21 '14 at 15:46

11 Answers11

1451

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.

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • 26
    Note that you should not put the NewColumnName in brackets, as SQL Server will then use [[[NewColumnName]]] as the name of the new column. Also, sp_rename should be prefixed by EXEC or EXECUTE. – Mark Freeman Nov 22 '13 at 15:20
  • 32
    You are allowed and encouraged to put brackets in the first parameter, identifying the column, but not in the second parameter. Like this: `EXEC sp_RENAME '[TableName].[OldColumnName]', 'NewColumnName', 'COLUMN'` – Niels Brinch Sep 03 '14 at 16:40
  • 3
    You should note in your answer that this stored procedure handles default values, null etc **contraints** while doing the rename, as opposed to a plain `alter table` that will FAIL if such constraints exist. – Tuncay Göncüoğlu Aug 23 '17 at 12:06
  • 6
    Note that if you use 'table_name.new_name' then it becomes [schema].[table_name].[table_name.new_name] - so don't put that table name in the new name! (it is correct in this answer, just adding a note for peeps) – Mark Schultheiss Nov 14 '17 at 19:50
  • 2
    If there is a schema in database you have to put schema name before table name: EXEC sp_RENAME 'schema_name.table_name.old_name', 'new_name', 'COLUMN' – amin Apr 23 '19 at 08:15
  • Use Transact SQL: `EXEC sp_rename 'dbo.department.dept_id', 'id', 'column';` { Where Table name = department dept_id = old column name and id = new column name } Reference documentation: https://learn.microsoft.com/en-us/sql/relational-databases/tables/rename-columns-database-engine?view=sql-server-ver15#TsqlProcedure – Damilola Abioye Jan 16 '22 at 03:39
134

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 :)

Community
  • 1
  • 1
Carrie Kendall
  • 11,124
  • 5
  • 61
  • 81
  • That is a good answer, but where I am currently employed, they have turned off the security to be able to do this. You can bring up the table in design mode, but the right to actually design or change it has been removed. – DataGirl Jun 10 '14 at 20:18
  • 1
    Or the user doesn't have the privileges. – Carrie Kendall Jun 10 '14 at 21:00
  • 6
    Never ever do that. You are copying the table, then dropping the old one and renaming it. NEVER use the GUI EVER to change anything about a table. – HLGEM Sep 16 '14 at 17:53
  • 7
    @HLGEM that's a pretty big blanket statement. In any case, can you provide any resources to what you've explained? ie dropping the table, etc. – Carrie Kendall Sep 18 '14 at 15:56
  • 3
    @CarrieKendall, script out the change from the GUI instead of making it and you will see. This is why is is much slower to make a change using the GUI to a large table than through using sp_rename or alter table. Further, changes to database structure are code changes and should be in source control like other code so should be in a script. This is especially important if you do not allow dev production database rights to change tables becasue you will need the script in any case. And you don't want to go about copying, dropping and recreating tables with millions of records on prod. – HLGEM Sep 18 '14 at 17:00
  • is there a way to build the script that it creates when following the directions in my answer? I am aware that the GUI, often times, can take slower or more dangerous routes but concerning this specifically I don't think it is the case. Without some proof, it is hard for me to believe that it isn't just using `sp_rename` under the hood. – Carrie Kendall Sep 18 '14 at 17:10
  • 22
    I just renamed a SQL Server 2012 table using Management Studio with profiler running and it used sp_rename. Can't speak for earlier versions though. – Steve Dowling Feb 17 '15 at 00:38
  • @HLGEM As @Steve Dowling pointed out, that's not true. "Generate change script..." reveals that renaming columns via the GUI merely calls `sp_rename`. I tested this in SQL Server 2005, 2012, and 2014. Minor caveat: it does call `sp_rename` twice per column instead of once, as discussed here: https://serverfault.com/questions/189691/why-does-sql-server-management-studio-generate-two-renames-in-a-change-script – MarredCheese Aug 28 '19 at 03:20
  • 1
    4th GUI way: click on the column in the tree and press F2. – MarredCheese Aug 28 '19 at 03:23
74

Try:

EXEC sp_rename 'TableName.OldName', 'NewName', 'COLUMN'
TechDo
  • 18,398
  • 3
  • 51
  • 64
40

You should also specify the schema of the table or you might get this error:

Msg 15248, Level 11, State 1, Procedure sp_rename, Line 238 Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.

If it is a deployment script I would also recommend adding some additional security to it.

IF EXISTS (
        SELECT 1
        FROM sys.columns
        WHERE
            name = 'OldColumnName' AND
            object_name(object_id) = 'TableName'
    ) AND
    NOT EXISTS (
        SELECT 1
        FROM sys.columns
        WHERE
            name = 'NewColumnName' AND
            object_name(object_id) = 'TableName'
    )
    EXEC sp_RENAME 'SchemaName.TableName.OldColumnName', 'NewColumnName', 'COLUMN';
MarredCheese
  • 17,541
  • 8
  • 92
  • 91
Taher
  • 573
  • 4
  • 7
25

Since I often come here and then wondering how to use the brackets, this answer might be useful for those like me.

EXEC sp_rename '[DB].[dbo].[Tablename].OldColumnName', 'NewColumnName', 'COLUMN'; 
  • The OldColumnName must not be in []. It will not work.
  • Don't put NewColumnName into [], it will result into [[NewColumnName]].
HonzaB
  • 7,065
  • 6
  • 31
  • 42
22

You can use sp_rename to rename a column.

USE YourDatabase;  
GO  
EXEC sp_rename 'TableName.OldColumnName', 'NewColumnName', 'COLUMN';  
GO  

The first parameter is the object to be modified, the second parameter is the new name that will be given to the object, and the third parameter COLUMN informs the server that the rename is for the column, and can also be used to rename tables, index and alias data type.

Alexandre Neukirchen
  • 2,713
  • 7
  • 26
  • 36
20

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.

Kols
  • 3,641
  • 2
  • 34
  • 42
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • * In step 1 NULL has to be allowed in new column * Optionally add 4th step to change new column, that it does not allow NULL values – BitLauncher Sep 10 '19 at 13:11
6

Improved version of @Taher

DECLARE @SchemaName AS VARCHAR(128)
DECLARE @TableName AS VARCHAR(128)
DECLARE @OldColumnName AS VARCHAR(128)
DECLARE @NewColumnName AS VARCHAR(128)
DECLARE @ParamValue AS VARCHAR(1000)

SET @SchemaName = 'dbo'
SET @TableName = 'tableName'
SET @OldColumnName = 'OldColumnName'
SET @NewColumnName = 'NewColumnName'
SET @ParamValue = @SchemaName + '.' + @TableName + '.' + @OldColumnName

IF EXISTS
(
    SELECT 1 FROM sys.columns WHERE name = @OldColumnName AND OBJECT_NAME(object_id) = @TableName
)
AND NOT EXISTS
(
    SELECT 1 FROM sys.columns WHERE name = @NewColumnName AND OBJECT_NAME(object_id) = @TableName
)
BEGIN
    EXEC sp_rename @ParamValue, @NewColumnName, 'COLUMN';
END
Rikin Patel
  • 8,848
  • 7
  • 70
  • 78
4

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.

Tahir77667
  • 2,290
  • 20
  • 16
1

Or you could just slow-click twice on the column in SQL Management Studio and rename it through the UI...

Dave Cole
  • 2,446
  • 2
  • 20
  • 26
-4

Run Query:

    SP_RENAME '[TableName].[ColumnName]','NewNameForColumn'