labwangsf 发表于 2013-1-13 18:54:28

数据库表间数据复制及多表间的数据读取

1、基本方法:From 后面可以接多个表名,表与表之间用逗号隔开,查询字段之间要加上表的名字。

例如:Select table1.column1, table2.column1 from table1, table2

2、表别名:可以为表设置别名,以简化输入,方法是直接在表名后加空格,接你设定的别名。

例如:Select a.column1, b.column2 from table1 a, table2 b

3、运算:选取的过程也可以进行一些简单运算,包括逻辑运算和数学运算。

例如:select isnull(a.column1, b.column2), case a.column2 when true then a.column3 else a.column4 from table1 a, table2 b

4、字段别名:运算结果给成的新字段是没有字段名的,为了提取方便,最好给它们设定别名,就像给正常的字段设定别名一样。

例如:select isnull(a.column1, b.column2) as column_status from table1 a, table2 b

对上面的基本方法进行终合运用,基本上已经可以满足多表联合查询的需求了。



报错为:
仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 'TableName' 中为标识列指定显式值。
SET IDENTITY_INSERT del ON
insert into del(id,birthday) select id,birthday from del --标明字段名
SET IDENTITY_INSERT del OFF

以上语句才可以正常复制数据从一表到另一个表

数据库表间数据复制分类

数据库表间数据复制分类:默认栏目
       在利用数据库开发时,常常会将一些表之间的数据互相导入。当然可以编写程序实现,但是,程序常常需要开发环境,不方便。最方便是利用sql语言直接导入。既方便而修改也简单。以下就是导入的方法。

1、表结构相同的表,且在同一数据库(如,table1,table2)

Sql :insert into table1 select*   from table2 (完全复制)

         insert into table1 selectdistinct*from table2(不复制重复纪录)

         insert into table1 selecttop 5 *fromtable2 (前五条纪录)

2、   不在同一数据库中(如,db1 table1,db2 table2)

sql:   insert into db1..table1 select*   from db2..table2 (完全复制)

         insert into db1..table1 selectdistinct*from db2table2(不复制重复纪录)

         insert into tdb1..able1 selecttop 5 *fromdb2table2 (前五条纪录)
页: [1]
查看完整版本: 数据库表间数据复制及多表间的数据读取