Solved

Mass rename SQL tables

Posted on 2013-01-26
7
964 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 167 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 333 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 52

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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 9

Accepted Solution

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

Expert Comment

by:mimran18
ID: 38824820
0
 
LVL 52

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

828 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