Rename column SQL Server 2008

前端 未结 12 2037
名媛妹妹
名媛妹妹 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:22

    Run Query:

        SP_RENAME '[TableName].[ColumnName]','NewNameForColumn'
    
    0 讨论(0)
  • 2020-11-27 09:25

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

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

    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]].
    0 讨论(0)
  • 2020-11-27 09:27

    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.

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

    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
    
    0 讨论(0)
  • 2020-11-27 09:28

    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';
    
    0 讨论(0)
提交回复
热议问题