1.A数据库的某张表有些数据,B数据库表是新建立的,执行一下sql语句把A数据库的某张表数据传到B数据库某张表
insert into A库.dbo.表名(字段名)
select top 10 B库字段名(可以* 前提是两个表字段全部一样) from B库.dbo.表名
where PersonID not in(select top 10 PersonID from YLMES_ZS.dbo.a_PersonInfo ) --- and UserID='1332' 可加where条件
2.A数据库的某张表有些数据,B数据库表是新建立的,执行一下sql语句或存储过程把A数据库的某张表数据传到B数据库某张表,可A数据库的某张表数据每天都是增加的,后来增加的数据怎么传到B数据库 还有在A数据库修改或删除了表的某条数据,怎么实现B数据库的表也修改删除 A库上的表加个触发器
一是采用触发器.虽然跨库操作用触发器有点怕效率差,但这是没办法的事情.
二是,定期将B库中的表清除,然后将A库中的表导入到B库,相当于执行一次存储过程.
create trigger tr_name on A.dbo.表1
for insert,update,delete
as
begin
delete B.dbo.表1 where id in (select id from deleted)
insert into B.dbo.表1
select * from inserted
end
3.如果是插入行或删除行,还可以考虑用存储过程来处理:
假设 A 数据库中 表 tb 有标识列(唯一) id,要将表中新插入的数据导入 B 数据库的表 tb,把A库TB表中已经删除的行在B 库中也删除,则可以这样:
create procedure updatedbB
as
begin
insert into B.dbo.tb
select a.* from tb a
where not exists(select 1 from B.dbo.tb where id=a.id)
delete from B.dbo.tb b
where not exists(select 1 from tb where id=b.id)
end