我将源服务器作为ABC,将目标服务器作为XYZ
我需要将一些表从ABC复制到XYZ,但是我不需要所有记录,我只需要一天的数据,并且每天都需要刷新?
有人可以建议如何做吗?
答案 0 :(得分:1)
如果两个数据库为on same server
,则可以按照以下步骤操作:
truncate XXXTable; --clear XXXTable
insert into XXXTable select * from XYZ..XXXTable; --copy
如果您的两个数据库为not on same server
,则可以按照以下步骤操作:
dblink
。EXEC master.dbo.sp_addlinkedserver @server = N'Server ', @srvproduct=N'SQL Server' ;
GO
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'dblinkName',@useself=N'False',@locallogin=NULL,@rmtuser=N'sa',@rmtpassword='########';
GO
truncate XXXTable; --clear XXXTable
insert into XXXTable select * from [dblinkname].XYZ..XXXTable; --copy
如何只复制一天的数据
您必须添加一个createtime列,并使用sysdate -1 day
过滤一天的数据,例如:
CREATE TABLE T1 ([val] int, [createDate] datetime) ; INSERT INTO T1 ([val], [createDate]) VALUES (1, '2019-11-03 00:00:00'), (2, '2019-11-03 00:00:00'), (3, '2019-11-02 00:00:00'), (4, '2019-11-02 00:00:00'), (5, '2019-11-01 00:00:00') ; GO
5 rows affected
CREATE TABLE T2 ([val] int, [createDate] datetime) ; GO
select Convert(date, dateadd(day,-1,getdate())) GO
| (No column name) | | :------------------ | | 02/11/2019 00:00:00 |
insert into T2 select * from T1 where createDate = Convert(date, dateadd(day,-1,getdate())) GO
select * from T2 GO
val | createDate --: | :------------------ 3 | 02/11/2019 00:00:00 4 | 02/11/2019 00:00:00
db <>提琴here
答案 1 :(得分:0)