(Transfer) SQL copy data table (select * into and insert into)

select * into target table name from source table name

insert into target table name (fld1, fld2) select fld1, 5 from source table name The

above two sentences are to insert the data of the source table into the target table, but the two sentences are different :

The first sentence (select into from) requires that the target table does not exist, because it will be automatically created when inserting.
The second sentence (insert into select from) requires the target table to exist. Since the target table already exists, we can insert constants in addition to the fields of the source table, as in the example: 5.



1: Copy the table structure and data to the new table

select * into destination database name. dbo. Destination table name from original table name

select * into my0735home.dbo.infoMianTest from infoMian

2: Backup some columns of the table (write out without writing * list of columns)

select column name 1, column name 2, column name 3 into destination database name. dbo. destination table name from original table name

select id, title, mtype, stype, author, tel, nr into infoMianTest2 from infomian

3: Part of the rows of the backup table (with WHERE condition)

select * into destination database name. dbo. destination table name from original table name where id<10

select * into infomiantest2 from infomian where id<10

4: Part of the column of the backup table (write out the list of columns without writing *) and part of the row (add WHERE condition)

select column name 1, column name 2, column name 3 into destination database name. dbo. destination table name from original Table name where id<10

5: Only copy the structure of the table: such as: SELECT * INTO t1 FROM titles WHERE 1=2

6: The query results come from multiple tables: such as:

SELECT title_id, title, pub_name INTO t3

FROM titles t INNER JOIN publishers p

ON t.pub_id=p.pub_id

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326169448&siteId=291194637