SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to rename a column in all tables and views


How to rename a column in all tables and views

Author
Message
babak3334000
babak3334000
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 62
Hi
I want to change the name of a column in all tables and views

(rename Feild "Phone" To "TelePhone")

Sp-ReName Change only one table can be done

Plz Help Me
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62029 Visits: 17954
babak3334000 (10/7/2013)
Hi
I want to change the name of a column in all tables and views

(rename Feild "Phone" To "TelePhone")

Sp-ReName Change only one table can be done

Plz Help Me


You first have to identify which table(s) contain that column. You can use sys.columns to find them. Then you will need to change the name in ALL of the tables.

For your views, you will have to alter every single view. There just isn't a shortcut for this but using the sys catalogs can help minimize the effort.

The below query should help you identify everywhere you have a column named "Phone".


select *
from sys.objects so
join sys.columns sc on so.object_id = sc.object_id
where sc.name = 'Phone'



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
babak3334000
babak3334000
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 62
After finding the table name command to rename writing
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62029 Visits: 17954
babak3334000 (10/7/2013)
After finding the table name command to rename writing


I think you mean how do you rename the column once you find it?

You mentioned the proc in your first post (sp_rename).

http://technet.microsoft.com/en-us/library/ms188351.aspx

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
babak3334000
babak3334000
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 62
I mean that this command can not be used (Sp-rename)

It does not matter to me what can be done with the command

Just change the column name in all tables should be

Plz Help Me Again
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62029 Visits: 17954
babak3334000 (10/7/2013)
I mean that this command can not be used (Sp-rename)

It does not matter to me what can be done with the command

Just change the column name in all tables should be

Plz Help Me Again


Why can you not use sp_rename???

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)SSC Guru (222K reputation)

Group: General Forum Members
Points: 222135 Visits: 46285
babak3334000 (10/7/2013)
Just change the column name in all tables should be


The way you change column names in a table is with sp_rename. There's no command that automagically changes the column name in all tables, you have to do it one by one with sp_rename.

I suppose you could drop and recreate all the tables to change the column names, but I suspect that's a little more work.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


babak3334000
babak3334000
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 62
The Name tables should be placed as one

EXEC sp_rename N'TableName1.Phone', N'Telephone', N'COLUMN';
EXEC sp_rename N'TableName2.Phone', N'Telephone', N'COLUMN';

There are many Tables and Views
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62029 Visits: 17954
babak3334000 (10/7/2013)
The Name tables should be placed as one

EXEC sp_rename N'TableName1.Phone', N'Telephone', N'COLUMN';
EXEC sp_rename N'TableName2.Phone', N'Telephone', N'COLUMN';

There are many Tables and Views


That looks to my like it would work just fine for these two tables. You could use the original query that I posted to help you build your sql. As has been said by both myself and Gail, there is no shortcut here. You are going to have to run this command on all the tables with the column you want to rename.

Something like this?


select 'EXEC sp_rename N''' + sch.name + '.' + so.name + '.Phone'', N''TelePhone'', N''COLUMN'';',
*
from sys.objects so
join sys.columns sc on so.object_id = sc.object_id
join sys.schemas sch on sch.schema_id = so.schema_id
where sc.name = 'Phone'




--EDIT--

Fixed a typo in the code.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
babak3334000
babak3334000
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 62

select 'EXEC sp_rename N''' + sch.name + '.' + so.name + '.Phone'', N''TelePhone'', N''COLUMN'';',
*
from sys.objects so
join sys.columns sc on so.object_id = sc.object_id
join sys.schemas sch on sch.schema_id = so.schema_id
where sc.name = 'Phone'


Do not Crying
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search