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.
| Author |
Topic |
|
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2006-07-20 : 09:31:05
|
| Hi,Is there a way to rename column like below?ALTER TABLE table_name RENAME COLUMN from_column TO to_column the statement above i found it from a web site, did not worked... |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-07-20 : 09:33:24
|
use sp_renameExample from BOLquote: B. Rename a columnThis example renames the contact title column in the customers table to title.EXEC sp_rename 'customers.[contact title]', 'title', 'COLUMN'
KH |
 |
|
|
raysefo
Constraint Violating Yak Guru
260 Posts |
Posted - 2006-07-20 : 09:50:13
|
| Hi,I wanna do it like below but gives error Line 13: Incorrect syntax near '+'.declare @name varchar(100)DECLARE table_create CURSOR FORselect Distinct (name) from sysobjects where xtype = 'U' and name != 'dtproperties' OPEN table_createFETCH NEXT FROM table_create into @nameWHILE @@FETCH_STATUS = 0BEGINEXEC sp_rename ''+@name+'.[pID]', 'x', 'COLUMN' FETCH NEXT FROM table_create into @nameENDCLOSE table_createDEALLOCATE table_create |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-07-20 : 09:55:34
|
| yukFETCH NEXT FROM table_create into @nameWHILE @@FETCH_STATUS = 0BEGINselect @obj = @name + '.[pID]'EXEC sp_rename @obj, 'x', 'COLUMN'FETCH NEXT FROM table_create into @nameEND==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2006-07-24 : 08:38:44
|
| e.g.Exec SP_Rename '<TableName.CurrentColumnName>' , '<NewColumnName>'(KHTan, is it necessary to mention object after NewColumnName? As it mentioned in BOL. but i have tried without object name)Mahesh |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-07-24 : 09:12:33
|
| "is it necessary to mention object after NewColumnName"It avoids possible ambiguityKristen |
 |
|
|
|
|
|
|
|