Rename multiple tables based on create_date

Posted on 2013-01-09
Last Modified: 2013-01-09
I have multiple tables that were created on a specific date. They are currently dbo.whatevername. I want them to be xxxlib.whatevername. How do I write sql code to accomplish this?
Question by:qbjgqbjg
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 22

Accepted Solution

Steve Wales earned 250 total points
ID: 38759310
First thing to do would be to create the schema xxxlib (if it doesn't already exist).

I'm sure you could write code to copy the tables all automatically.   If I had to perform a copy like this, I'd want to generate the code to do it and carefully review it before executing it.

So, something like this (probably not the prettiest way to do it, but I personally prefer to carefully review such things before executing):

select 'select * into xxxlib.'+name+' from dbo.'+name+';'
from sys.tables
where schema_id = 1
and convert(char(8), create_date, 112) = '20130103'

Open in new window

Hit Control-T in SSMS to sent output to text, then execute.

In the output window you have a script you can verify for correctness before running.

Make sure your users are locked out and what not before executing such a thing and of course you would need to make sure proper permissions were set on the new tables.
LVL 41

Assisted Solution

ralmada earned 250 total points
ID: 38759375
It seems to me that you want to rename the schema, not create copies of the tables from one schema to another. I would use the ALTER SCHEMA then. Run:

declare @strSQL varchar(max)
set @strSQL = ' '
SELECT @strSQL = @strSQL + 'ALTER SCHEMA newschema TRANSFER dbo.' + quotename(name) + ';'
FROM sys.objects
WHERE schema_id = 1

print @strSQL -- to check the intended output is correct

-- execute(@strSQL) --uncomment to make the changes once you're sure.

Author Closing Comment

ID: 38759524

Expert Comment

ID: 38759547
You can try the following script

--USE Test01


set @Change = 'ALTER SCHEMA TestSchema TRANSFER dbo.['

	 @SQL = COALESCE(@SQL + @Change + [name] + '] ', @Change + [name] + '] ')
	--create_date = Your condition



Open in new window


Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

724 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