Skip to content

Instantly share code, notes, and snippets.

@MarkPryceMaherMSFT
Created May 6, 2022 15:58
Show Gist options
  • Save MarkPryceMaherMSFT/e9315d2b062d5d54d63fc89abdea1874 to your computer and use it in GitHub Desktop.
Save MarkPryceMaherMSFT/e9315d2b062d5d54d63fc89abdea1874 to your computer and use it in GitHub Desktop.
This script uses two procedures; create_external_table.sql and export_all_tables. It uses the control table from export_all_tables to drive the imports and hold all the metadata,
create proc import_data as
begin
declare @dummy_datetime datetime2 ;
declare @sSQL varchar(8000);
if exists (
select 1 from sys.schemas s inner join sys.tables t
on s.schema_id = t.schema_id
where s.name = 'dbo' and t.name = 'tmp_tables_import' )
BEGIN
PRINT 'DELETE TABLE'
set @sSQL = 'DROP TABLE [dbo].[tmp_tables_import];'
EXEC (@sSQL);
END
ELSE
BEGIN
PRINT 'TABLE DOES NOT EXISTS'
END
create table dbo.tmp_tables_import with (distribution=round_robin,heap)
as select *, @dummy_datetime as import_start_date, @dummy_datetime import_end_date from dbo.tmp_tables
set @sSQL = ''
select * from dbo.tmp_tables_import ;
DECLARE
@i INT = 1
, @t INT = (SELECT COUNT(*) FROM dbo.tmp_tables_import)
,@schema_name varchar(255)
,@table_name varchar(255)
,@state varchar(255)
,@ext_table_name varchar(500)
,@data_source varchar(500)
,@FILE_FORMAT varchar(500)
,@Location varchar(500)
,@import_start_date datetime2
,@import_end_date datetime2
WHILE @i <= @t
BEGIN
select @schema_name = [schema_name]
,@table_name=[table_name]
,@state =[state]
,@ext_table_name=[ext_table_name]
,@data_source=[data_source]
,@FILE_FORMAT=[FILE_FORMAT]
,@Location=[Location]
from dbo.tmp_tables_import
where @i = seq_id
set @import_start_date = getdate();
if @state = 'Done'
begin
begin try
print 'Creating external table'
update dbo.tmp_tables_import set state = 'Creating external table', import_start_date = @import_start_date where @i = seq_id
exec [dbo].[create_external_table_from_table] @table_name, @schema_name,
@data_source,@Location,@FILE_FORMAT ,@ext_table_name,@schema_name
update dbo.tmp_tables_import set state = 'Created external table', import_start_date = @import_start_date where @i = seq_id
print 'Before insert statement'
set @sSQL = 'INSERT INTO [' + @schema_name + '].[' + @table_name + '] SELECT * FROM [' + @schema_name + '].[' + @ext_table_name + ']'
print @sSQL
exec (@sSQL)
update dbo.tmp_tables_import set state = 'Inserted data', import_start_date = @import_start_date where @i = seq_id
print 'Imported'
set @state = 'Imported'
end try
begin catch
set @state = 'Error:' + ERROR_MESSAGE();
end catch
end
set @import_end_date = getdate();
update dbo.tmp_tables_import set state = @state, import_end_date = @import_end_date where @i = seq_id
SET @i+=1;
END
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment