Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

How to rename a column in all tables and views Expand / Collapse
Author
Message
Posted Monday, October 7, 2013 2:19 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, October 7, 2013 2:46 PM
Points: 23, Visits: 60

This is done with the command ??

execsp_msforeachtable N'
IF EXISTS(SELECT 1 FROM sys.columns WHERE object_id=OBJECT_ID(''?'') AND name = ''Col1'')
EXEC (''UPDATE ? SET Col1= case when Col1 = ''''ppp'''' then ''''qqq'''' when Col1 = ''''aaa'''' then ''''xxx'''' end'')'
Post #1502362
Posted Monday, October 7, 2013 2:24 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,282, Visits: 12,116
babak3334000 (10/7/2013)

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


????


_______________________________________________________________

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 Moden's 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)
Post #1502363
Posted Monday, October 7, 2013 2:25 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,282, Visits: 12,116
babak3334000 (10/7/2013)

This is done with the command ??

execsp_msforeachtable N'
IF EXISTS(SELECT 1 FROM sys.columns WHERE object_id=OBJECT_ID(''?'') AND name = ''Col1'')
EXEC (''UPDATE ? SET Col1= case when Col1 = ''''ppp'''' then ''''qqq'''' when Col1 = ''''aaa'''' then ''''xxx'''' end'')'


NO. Use the code I posted to help you build the sql you need to execute. I can't figure out what you are trying to do here with sp_msforeachtable. That code is updating the table, it has nothing to do with renaming the column.


_______________________________________________________________

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 Moden's 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)
Post #1502364
Posted Monday, October 7, 2013 2:33 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, October 7, 2013 2:46 PM
Points: 23, Visits: 60
Sean Lange (10/7/2013)
babak3334000 (10/7/2013)

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


????

It does not work
the name of the columns did not change
Post #1502370
Posted Monday, October 7, 2013 2:37 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,282, Visits: 12,116
babak3334000 (10/7/2013)
Sean Lange (10/7/2013)
babak3334000 (10/7/2013)

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


????

It does not work
the name of the columns did not change


Of course not...it is just a select statement. It does however have all of the sql you need to execute in the first column. Run the query, select the first column, paste that into a window and it should be the rename for your tables. You will probably want to add "and so.type = 'U'" to the query so it only returns tables. You will have to manually update all of your views.


_______________________________________________________________

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 Moden's 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)
Post #1502375
Posted Monday, October 7, 2013 2:46 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, October 7, 2013 2:46 PM
Points: 23, Visits: 60
Thank You So Much Master

Always be healthy " Sean Lange "
Post #1502376
Posted Monday, October 7, 2013 2:58 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,282, Visits: 12,116
babak3334000 (10/7/2013)
Thank You So Much Master

Always be healthy " Sean Lange "


Glad you were able to figure it out.


_______________________________________________________________

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 Moden's 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)
Post #1502382
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse