Solved

Mass rename SQL tables

Posted on 2013-01-26
7
947 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now