Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Mass rename SQL tables

Posted on 2013-01-26
7
Medium Priority
?
1,041 Views
Last Modified: 2013-01-27
I have inherited a db with tables named like

dbo.xyz_aaaa
dbo.xyz_bbb
dbo.abc_cccc
dbo.abc_dddd
dbo.eeee
dbo.ffff

Everything to the right of the underscore is unique and I want to end up with

dbo.aaaa
dbo.bbb
dbo.cccc
dbo.dddd
dbo.eeee
dbo.ffff

Is there a way to use sql server to mass update dbo.xyz_aaaa to dbo.aaaa including any  Views or Stored procedures.   Or do I just use the generate scripts function, use the editor to do to a find and replace, then recreate?
0
Comment
Question by:Scott Fell,  EE MVE
  • 4
  • 2
7 Comments
 
LVL 9

Assisted Solution

by:BlueYonder
BlueYonder earned 668 total points
ID: 38822607
A script is possible. Use the following for the table names and a cursor to traverse the each table.

SELECT name
FROM dbo.sysobjects
WHERE xtype = 'U' AND --THE NAME OF THE DB--



ALTER TABLE table_name
RENAME TO new_table_name;
0
 
LVL 9

Assisted Solution

by:mimran18
mimran18 earned 1332 total points
ID: 38822635
TRY THIS

SELECT 'ALTER TABLE ' + NAME +  ' RENAME TO ' + Substring(name ,CharIndex('_',name)+1,LEN(name))
FROM dbo.sysobjects
WHERE xtype = 'U' And CharIndex('_',name)>0
0
 
LVL 54

Author Comment

by:Scott Fell, EE MVE
ID: 38822640
Thank you.  What about the find and replace for the table names in views and stored procedures?
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 9

Accepted Solution

by:
mimran18 earned 1332 total points
ID: 38822647
Select * from syscomments Where [text] like '%tablename%'
0
 
LVL 9

Expert Comment

by:mimran18
ID: 38824820
0
 
LVL 54

Author Comment

by:Scott Fell, EE MVE
ID: 38824838
Thanks, I got it done.  For the views and stored procs I just generated the script and did a find and replace.
0
 
LVL 9

Expert Comment

by:mimran18
ID: 38824880
You are Welcome
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question