Solved

Rename multiple tables based on create_date

Posted on 2013-01-09
4
377 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
4 Comments
 
LVL 22

Accepted Solution

by:
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.
0
 
LVL 41

Assisted Solution

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

18 Experts available now in Live!

Get 1:1 Help Now