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.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*