SQL Select * into from

If you want to copy or backup your table in the database, use the following SQL command.

select * into newtable
from oldtable

The new table is created on the fly. You do not need to create it. A more descriptive example is below. Not that # means temporary table.

create table #table1(
id int not null primary key identity,
name varchar(15),
fav_game varchar(20)

insert into #table1 values('john','hockey')
insert into #table1 values('Kim','footbal')
insert into #table1 values('Joe','soccer')

select * from #table1

— at this point only #table1 exists
— lets say want to want to backup table1, we will use

select * into #table2
from #table1

At this point we will have #table2, which will be exactly same as table#. We can create a select query against #table2.

This entry was posted in SQL Server 2008. Bookmark the permalink.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s