Monday, March 26, 2012

Rename Table

How do you rename a column in SQL I have tried everything.Lookup sp_rename in the BOL
Thomas
"Lontae Jones" <LontaeJones@.discussions.microsoft.com> wrote in message
news:809E5B93-49BA-479D-B5A7-A9F9B18FDF04@.microsoft.com...
> How do you rename a column in SQL I have tried everything.|||Use System Stored Proc called sp_rename
sp_rename [ @.objname = ] 'object_name' ,
[ @.newname = ] 'new_name'
[ , [ @.objtype = ] 'object_type' ]
"Lontae Jones" wrote:

> How do you rename a column in SQL I have tried everything.|||Do you want to rename a table or a column? Your subject says table but your
messages says column.
To rename a column, it can be confusing. You have to specify both the table
and column name for the old name, but for the new name you just specify the
column name:
exec sp_rename 'table_name.original_column_name', 'new_column_name'
HTH
--
Kalen Delaney
SQL Server MVP
www.SolidQualityLearning.com
"Lontae Jones" <LontaeJones@.discussions.microsoft.com> wrote in message
news:809E5B93-49BA-479D-B5A7-A9F9B18FDF04@.microsoft.com...
> How do you rename a column in SQL I have tried everything.|||Here is a short example for you.
Thanks
set nocount on
go
create table t(col int)
go
insert t values(1)
insert t values(2)
insert t values(3)
insert t values(4)
go
select * from t
go
exec sp_rename 't','u'
go
print 'renamed table u'
select * from u
go
print 'renamed column col as newcol'
EXEC sp_rename 'u.col', 'newcol', 'COLUMN'
select * from u
go
drop table u
go
http://zulfiqar.typepad.com
MCP
"Lontae Jones" wrote:

> How do you rename a column in SQL I have tried everything.

No comments:

Post a Comment