当前位置: 代码迷 >> Sql Server >> 数据库中表数据安插到另外数据库的两个表中
  详细解决方案

数据库中表数据安插到另外数据库的两个表中

热度:42   发布时间:2016-04-27 12:08:21.0
数据库中表数据插入到另外数据库的两个表中
表A(属于db1) :id title content
表B:(属于db2):id title
表C:(属于db2):id content
要求:将表A中的title插入到表B中,表A中的content插入到表C中,表B的id是主键,自增。表C中的id是主键,不自增,执行插入的同时B的id=C的id。

------解决方案--------------------
SQL code
insert into bselect titlefrom ainsert into cselect b.id,a.contentfrom a join b on a.title = b.title--a中的title没有重复值!
------解决方案--------------------
insert into b(title) select title from a
insert into c(content) select content from a
------解决方案--------------------
DECLARE @ID INT
SELECT @ID=MAX(ID) FROM B
INSERT B SELECT TITEL FROM A
INSERT C SELECT * FROM B WHERE ID>@ID
  相关解决方案