Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generate backup on new schema iterating over certain tables – T-SQL

I got a list of table names from sys.tables that looks like this:

SELECT name
FROM sys.tables
WHERE name LIKE 'ETL%'

I want to move to a new schema (kinda like a backup) and I was thinking of using something like the following query being @table_name the current item from the list:

SELECT *
INTO [historic].[@table_name]
FROM @table_name

However I cannot seem to sort out how I can make this kind of iterator.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re probably only going to perform this once, you can use the following example query to construct the SQL statements needed, you can copy and paste into an SSMS window to use.

select Concat(
  'select * ',
  'into [historic].', QuoteName(name),
  ' from ', QuoteName(name), ';'
)
from sys.tables
where name like 'ETL%' and Schema_Name(schema_id) = 'dbo';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading