Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Rename multiple tables based on create_date

Posted on 2013-01-09
4
Medium Priority
?
385 Views
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?
0
Comment
Question by:qbjgqbjg
[X]
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
4 Comments
 
LVL 23

Accepted Solution

by:
Steve Wales earned 1000 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.
0
 
LVL 41

Assisted Solution

by:ralmada
ralmada earned 1000 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.
0
 

Author Closing Comment

by:qbjgqbjg
ID: 38759524
Thanks
0
 
LVL 5

Expert Comment

by:RehanYousaf
ID: 38759547
You can try the following script

--CREATE SCHEMA TestSchema
--USE Test01
--GO

DECLARE @SQL VARCHAR(MAX)
DECLARE @Change VARCHAR(MAX)

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

SELECT
	 @SQL = COALESCE(@SQL + @Change + [name] + '] ', @Change + [name] + '] ')
FROM
	sys.tables
--WHERE
	--create_date = Your condition

PRINT @SQL

EXEC (@SQL)  

Open in new window

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 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