05.30.08

Query to Change Column Name in sql server

Posted in Important SQL Query, Interview Questions, SQL Server tagged , at 6:58 am by dayananthan

I have created an Table Test with wrong column name as EepId instead of EmpID, and now i want to change it to EmpID, the following is the query to get

create table Test

(

EepID int

)

 insert into Test values(2)

 Now I want EepID column to Change to EmpID. The following is the solution

 EXEC sp_rename

    @objname = ‘Test.EepID’,

    @newname = ‘EmpID’,

    @objtype = ‘COLUMN’

6 Comments »

  1. sunny pandit said,

    But what to do when we need to change column name as well as its type ??

  2. ushant said,

    is there no any other method to solve this query.

  3. amit sharma said,

    sir ,
    may i know how to change a column name in sql 2000. I tried
    –alter table t1 alter column t1_phno varchar(10) to phno varchar(10)– but it is not working ..

    can anybody help .

    thanks a lot..
    amit sharma

  4. amit sharma said,

    yes i found it and it is for how to change the column name in sql……

    exec sp_rename ‘t1.[t1_name]‘,’name’,'column’

    where t1 is the table name ,and t1_name is the column name which is to be changed, and “name” it is name after change…………..

    thanks and enjoy..

  5. Yas said,

    Thanks it really helped :)

  6. Anuj said,

    exec sp_rename ‘t1.[t1_name]‘,’name’,’column’


Leave a Comment