Copy the SQL data tables and table structure

 

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

The above two are 'in the source table' data into the "destination table", but there are two differences:

The first sentence (select into from) requires that the target table does not exist, because it will be created automatically upon insertion.

The second sentence (insert into select from) requirements of the target table exists, since the destination table already exists, we inserted the source table in addition to the fields, constants may also be inserted as in Example: 5.

 

Examples

There are the following data:

Now you want to copy data id is a 2 to the table, the following statement can be used:

insert into test select * from test where id=2;

The results are shown:

To now id 2 data are copied into the table, while the value of its name was changed to 'f', the following statement can be used:

insert into test(id,name) select id,'f' from test where id=2

The results are shown:

 


 In this regard, we can expand, the current statistics are as follows:

 

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

SELECT * INTO destination database name .dbo. The purpose of the original table name FROM table name

We test copy table structure and data to the new table test1:

SELECT * INTO test1 FROM test

The results are as follows:

 

2. Back up the column part of the table (do not write * and write the list of columns)

SELECT column names 1, 2 column names, column names, the name of the destination database 3 into .dbo. Original table name from the table name

We test1 table columns id, name replicate data to test2:

SELECT id,name INTO test2 FROM test1

The results are as follows:

 

3. The part of the backup row of the table (plus WHERE condition)

SELECT * INTO destination database name .dbo. The purpose of the original table name FROM table WHERE condition

We id 1 data copied from test2 to test3:

SELECT * INTO test3 FROM test2 WHERE id = 1

结果如下:

 

4.备份表的一部分列(不写*而写出列的列表)和一部分行(加WHERE条件)

SELECT 列名1,列名2,列名3 INTO 目的数据库名.dbo.目的表名 FROM 原表名 WHERE 条件

我们把在 id 大于0 时,数据表 test1 的列 id,age 的数据复制到 test4:

SELECT id,age INTO test4 FROM test1 WHERE id > 0

结果如下:

 

5.只复制表的结构

这种情况,我们只要设置查询条件不成立即可。我们把表 test1 的表结构复制到新表 test5:

SELECT * INTO test5 FROM test1 WHERE 1=2

结果如下:

 

6.复制的结构数据来源于多个表

其实我们要多个表的查询结构作为一个数据源就行了,我们先看看表 test2 和 test4 目前的结果和数据:

然后我再把 id 相同下的 id,name,age 数据复制到新表 test6:

SELECT id,name,age INTO test6 FROM (SELECT test2.id,name,age FROM test2 INNER JOIN test4 ON test2.id = test4.id) as a

结果如下:

select * into 目标表名 from 源表名
insert into 目标表名(fld1, fld2) select fld1, 5 from 源表名

以上两句都是将'源表'的数据插入到'目标表',但两句又有区别的:

第一句(select into from)要求目标表不存在,因为在插入时会自动创建。

第二句(insert into select from)要求目标表存在,由于目标表已经存在,所以我们除了插入源表的字段外,还可以插入常量,如例中的:5。

 

示例

目前有数据如下:

现在要将id为2的数据复制一条到表中,可用如下语句:

insert into test select * from test where id=2;

结果如图:

现在要将id为2的数据都复制到表中,同时把其 name 的值改为 'f',可用如下语句:

insert into test(id,name) select id,'f' from test where id=2

结果如图:

 


 对此,我们就可以扩充下,目前有数据如下:

 

1:复制表结构及数据到新表

SELECT * INTO 目的数据库名.dbo.目的表名 FROM 原表名

我们把表 test 的结构和数据复制到新表 test1:

SELECT * INTO test1 FROM test

结果如下:

 

2.备份表的一部分列(不写*而写出列的列表)

SELECT 列名1,列名2,列名3 into 目的数据库名.dbo.目的表名 from 原表名

我们把表 test1 列 id,name 的数据复制到 test2:

SELECT id,name INTO test2 FROM test1

结果如下:

 

3.备份表的一部分行(加 WHERE 条件)

SELECT * INTO 目的数据库名.dbo.目的表名 FROM 原表名 WHERE 条件

我们把 id 为1的数据从test2复制到 test3:

SELECT * INTO test3 FROM test2 WHERE id = 1

结果如下:

 

4.备份表的一部分列(不写*而写出列的列表)和一部分行(加WHERE条件)

SELECT 列名1,列名2,列名3 INTO 目的数据库名.dbo.目的表名 FROM 原表名 WHERE 条件

我们把在 id 大于0 时,数据表 test1 的列 id,age 的数据复制到 test4:

SELECT id,age INTO test4 FROM test1 WHERE id > 0

结果如下:

 

5.只复制表的结构

这种情况,我们只要设置查询条件不成立即可。我们把表 test1 的表结构复制到新表 test5:

SELECT * INTO test5 FROM test1 WHERE 1=2

结果如下:

 

6.复制的结构数据来源于多个表

其实我们要多个表的查询结构作为一个数据源就行了,我们先看看表 test2 和 test4 目前的结果和数据:

然后我再把 id 相同下的 id,name,age 数据复制到新表 test6:

SELECT id,name,age INTO test6 FROM (SELECT test2.id,name,age FROM test2 INNER JOIN test4 ON test2.id = test4.id) as a

结果如下:

Guess you like

Origin www.cnblogs.com/guorongtao/p/11607655.html