If you're doing a full data replacement then the synonym method is pretty straightforward:-- create original source datacreate table one(a int not null);insert one values(1);-- synonym for sourcecreate synonym source for one;-- only select from synonymselect * from source;-- load stagingcreate table stage(a int not null);insert stage values(2);-- swap sources via synonymbegin try begin tran; drop synonym source; create synonym source for stage; commit tran;end trybegin catch; rollback tran; raiserror('Synonym swap failed',16,1)end catchselect * from source;-- clean updrop synonym source;drop table one,stage;
If you're doing something else that's more involved you'll need to post details.Alternately you can do something similar using different schemas, then transfer/swap the objects:-- schema swapGO-- create schema for stagingcreate schema stage authorization dbo;GO-- create holding schema for swapcreate schema holding authorization dbo;GO--create tables and populatecreate table dbo.source(a int not null)create table stage.source(a int not null)insert dbo.source values(1);insert stage.source values(2);-- make sure no holding table existsif OBJECT_ID('holding.source') is not null drop table holding.source;-- always select from dboselect * from dbo.source;-- swap data (dbo->holding, stage->dbo, holding->stage)begin try begin tran; alter schema holding transfer dbo.source; alter schema dbo transfer stage.source alter schema stage transfer holding.source; commit tran;end trybegin catch; rollback tran; raiserror('Schema swap failed',16,1)end catchselect * from dbo.source;-- clean up drop table stage.source, dbo.source;if OBJECT_ID('holding.source') is not null drop table holding.source;
We use this at my current job, at the time we weren't using Enterprise Edition and needed to have the same effect as partition switching. This was the best way to manage while also keeping previous versions available, although the synonym method can do the same.You can read more about it here:http://sqlperformance.com/2012/08/t-sql-queries/t-sql-tuesday-schema-switch-a-roohttp://sqlperformance.com/2013/04/t-sql-queries/schema-switch-a-roo-part-2