none
T-Sql Change Column Name

Answers


  • Lookup sp_rename in BOL.
     
    USE AdventureWorks;  
    GO  
    EXEC sp_rename 'Sales.SalesTerritory.TerritoryID''TerrID''COLUMN';  
    GO 

    Use caution to chnage the SP's that refer to this column or your app will break.
    Sankar Reddy | http://sankarreddy.spaces.live.com/
    • Marked as answer by Mattaniah Friday, December 19, 2008 3:39 PM
    Friday, December 19, 2008 2:56 PM
    Moderator

All replies


  • Lookup sp_rename in BOL.
     
    USE AdventureWorks;  
    GO  
    EXEC sp_rename 'Sales.SalesTerritory.TerritoryID''TerrID''COLUMN';  
    GO 

    Use caution to chnage the SP's that refer to this column or your app will break.
    Sankar Reddy | http://sankarreddy.spaces.live.com/
    • Marked as answer by Mattaniah Friday, December 19, 2008 3:39 PM
    Friday, December 19, 2008 2:56 PM
    Moderator
  • Hi

    U can also use Alter table if u r in need to modify the datatype along with column name

    Code:
    ALTER TABLE Sales
    ALTER COLUMN ItemCount INT;
    Output:
    (3 row(s) affected)
    Explanation:

    This example changes the data type of the column 'ItemCount' to an integer (INT).

    Language(s): MS SQL Server




    the syntax is
    ALTER TABLE
    Syntax:
    ALTER TABLE table
    {
        ADD
        {
           column_name data_type [ ( size ) ]
           [ DEFAULT value ]
           { [ NULL | NOT NULL ] | [ { PRIMARY KEY | UNIQUE } ] }
           { [ CONSTRAINT constraint_name ] }
        }
        |
        ALTER COLUMN column_name
        {
           data_type [ ( size ) ] [ ( precision [ , scale ] ) ]
           [ NULL | NOT NULL ]
        }
        |
        DROP { COLUMN column_name | [ CONSTRAINT ] constraint_name }
    }
    table
    Specifies which table is to be altered.
    column_name
    The name of the column being added, altered, or dropped.
    data_type
    The data type of the column being added or altered.
    size
    Is the length of the data that can be entered into a field.
    DEFAULT value
    Is the default value for the column being altered.
    NULL | NOT NULL
    Is a parameter that indicates whether a column can or cannot contain null values.
    PRIMARY KEY
    Is a parameter that identifies the column or set of columns whose values uniquely identify each row in a table. Each table can only have one primary key constraint.
    UNIQUE
    Is a constraint that enforces the uniqueness of the values in a set of columns.
    constraint_name
    The name of the constraint to be added or dropped.
    precision
    Specifies the precision for the data type.
    scale
    Specifies the scale for the data type.

    The ALTER TABLE statement can be used to modify an existing table by adding, altering, or dropping columns and indexes.





    Thanks
    anlis
    • Proposed as answer by silna Wednesday, December 24, 2008 3:35 AM
    Saturday, December 20, 2008 9:08 AM
  • Hi,

    Re-Naming Column:

    USE AdventureWorks
    GO
    sp_RENAME 'Table_First.Name', 'NameChange' , 'COLUMN'
    GO

    Re-Naming Table:

    sp_RENAME 'Table_First', 'Table_Last'
    GO


    Thanks Shiven:) If Answer is Helpful, Please Vote

    Wednesday, July 18, 2012 5:39 AM