Transferring Data With The Switch Command

I ran across the switch command that can transfer data from one table to another. This assumes that the tables have the same columns in the source and in the target.

Here is an example:

create table Table1 (
RecordNumber int identity(1,1) not null primary key clustered,
DisplayName varchar(20) not null,
SortOrder int not null)
go

create table Table2 (
RecordNumber int identity(1,1) not null primary key clustered,
DisplayName varchar(20) not null,
SortOrder int not null)
go

insert into Table1(DisplayName, SortOrder) values ('Record1', 1);
insert into Table1(DisplayName, SortOrder) values ('Record2', 2);
insert into Table1(DisplayName, SortOrder) values ('Record3', 3);
insert into Table1(DisplayName, SortOrder) values ('Record4', 4);
go

select * from Table1;
select * from Table2;
go

alter table Table1 switch to Table2;
go

select * from Table1;
select * from Table2;
go
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: