locked
how to change column(field name) name in table using SQL Server? RRS feed

  • Question

  • I want to change old column name with new name in sql ? i tried using

    cmd = new SqlCommand("ALTER TABLE test_details sp_rename  column " + oldname + " to " + name + "", con);

    but it gives error:

    Incorrect syntax near the keyword 'column'.

    Plz help me ?

    Regards,

    Yogesh Koli,

    Web Developer,

    Aexonic Technologies Pvt Ltd.

    Friday, October 26, 2012 10:16 AM

Answers

  • Try the below:

    cmd = new SqlCommand("exec sp_rename   " + oldname + " , " + newname + ",COLUMN", con);


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    • Marked as answer by Yogesh Koli Friday, October 26, 2012 10:33 AM
    Friday, October 26, 2012 10:23 AM

All replies

  • try this :

    exec sp_rename 
    @objname = 'tablename.oldcolumnName',
    @newname = 'newcolumnname',
    @objtype = 'COLUMN'


    Please vote if you find this posting was helpful or Mark it as answered.

    Friday, October 26, 2012 10:20 AM
  • Here is the SQL query but be aware of the old column dependencies as it might break other things

    SP_RENAME <Schema_Name.Table_Name.Old_column_Name>,<New_Column_Name>,'COLUMN'


    Thanks and regards, Rishabh K



    • Edited by Rishabh K Friday, October 26, 2012 10:23 AM
    Friday, October 26, 2012 10:21 AM
  • Try the below:

    cmd = new SqlCommand("exec sp_rename   " + oldname + " , " + newname + ",COLUMN", con);


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    • Marked as answer by Yogesh Koli Friday, October 26, 2012 10:33 AM
    Friday, October 26, 2012 10:23 AM
  • Ways you can rename the columns are

    1.using sp sp_rename

    sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'

    2.In SSMS go to the table and double click the column name and rename it.


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful. By ganeshk

    Friday, October 26, 2012 10:26 AM
  • Thank you for replying me ?

    Regards,

    Yogesh Koli
    • Edited by Yogesh Koli Friday, October 26, 2012 10:34 AM
    Friday, October 26, 2012 10:33 AM
  • Thank you for replying me ?

    Regards,

    Yogesh Koli
    • Edited by Yogesh Koli Friday, October 26, 2012 10:34 AM
    Friday, October 26, 2012 10:33 AM
  • Thank you for replying me ?

    Regards,

    Yogesh Koli

    • Edited by Yogesh Koli Friday, October 26, 2012 10:34 AM
    Friday, October 26, 2012 10:34 AM