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
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Server - Looking to filter rows based on column value 3 37
SQL 2008 with .NET 4.5.2 4 29
Count with a subquery showing details 10 44
Linked Server Issue with SQL2012 3 26
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 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