日期:2014-05-17  浏览次数:20425 次

怎么用SQL把另外一个数据库的数据导入进来
A 数据库
YH表 字段:UID ExID


B 数据库
DeptList 表 字段:DeptID ExID
UserList 表 字段:UID DeptID

现在2个库中UID是统一的,ExID是统一的。

我想通过UserList 和YH 的UID关联,可以获取A 数据库ExID:UID---》ExID;然后在通过ExID,关联回DeptList表中,DeptID, 用得到的DeptID更新UserList表中DeptID


谢谢。



------解决方案--------------------
SQL code

Update a set a.DeptID=b.DeptID  
  from database_A.dbo.YH a, database_B.dbo.UserList  b, database_B.dbo.DeptList  c
    where a.UID=b.c.UID  and a.ExID=b.ExID

------解决方案--------------------
--如果A,B两库同机。

update b.dbo.UserList
set DeptID = n.DeptID
from b.dbo.UserList m , b.dbo.DeptList n , a.dbo.YH t
where m.UID = t.UID and t.ExID = n.ExID
 
--如果A,B两库不同机,做好连接后。

update b库所在机器名.b.dbo.UserList
set DeptID = n.DeptID
from b库所在机器名.b.dbo.UserList m , b库所在机器名.b.dbo.DeptList n , a库所在机器名.a.dbo.YH t
where m.UID = t.UID and t.ExID = n.ExID