Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Rename Column

Author  Topic 

kalyan.cse05
Yak Posting Veteran

74 Posts

Posted - 2012-07-17 : 03:07:41
I need to rename a column name of a table..How can i achieved that without using sp_RENAME..?

kalyan Ashis Dey

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-07-17 : 03:54:03
EXEC SP_RENAME table.column,'NewName','COLUMN'

(Only method in TSQL)

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

kalyan.cse05
Yak Posting Veteran

74 Posts

Posted - 2012-07-17 : 03:57:27
I know this..that's why i mentioned without "SP_Rename"..
Can someone plz help me...

kalyan Ashis Dey
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-07-17 : 04:25:32
Create a new column copy the column data into it.
Drop the old column


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

kalyan.cse05
Yak Posting Veteran

74 Posts

Posted - 2012-07-17 : 04:47:23
i beleive that will create the new column at the end of the table. i.e. if my table has 5 columns as field0,field1,field2,field3,field5 and if i need to rename "field1" as "fieldnew" then what should i do..as per your approach if i drop field1 and then create fieldnew then that fieldnew will be added after field5. But i don't want change the sequence of the columns....
Can you plz advice

kalyan Ashis Dey
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-07-17 : 05:05:34
Create Temporary Tabe, Insert data, drop orignal table, recreate table with new column name, move data from temp table.

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-07-17 : 05:11:34
rename the table and create a view with the table name selecting frrom the table with the columns in the order you want.

Also think about why your system is dependent on the column order and think about changing it.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-07-17 : 09:02:37
or do it via SSMS using Design option

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-07-17 : 09:33:44
quote:

Also think about why your system is dependent on the column order and think about changing it.


This!

Why do you care what order the columns are in? You should never be depending on column order in your code. All you INSERT or SELECT code should have a column list specified......


You could wrap the table in a view if you really, really wanted to.....

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page
   

- Advertisement -