具体的脚本程序:
/*用途:把第三方提供的数据信息时时导入我们自己的数据库中。
步骤:1、用 DTS (导入导出向导)把数据导入到中间数据库中,要设置任务定时导入。2、编写下面的脚本,然后把它加在 DTS 任务的后一步,即运行完 DTS后在运行此脚本。 */
declare @comid int,@comtitle nvarchar(255) --定义变量
declare test cursor for --定义游标
select DISTINCT comid,comtitle from dbA.dbo.infosociety where
datediff(day,comdate,getdate()) < 1
open test --打开游标
fetch next from test into @comid,@comtitle
while @@fetch_status = 0 --循环取数据
begin
declare @titletemp nvarchar(255)
declare test1 cursor for
select comtitle from dbB.dbo.tb_infogegu where comtitle=@comtitle and datediff(day,comdate,getdate()) = 0
open test1
fetch next from test1 into @titletemp
if( @@fetch_status = 0) --表中该记录已经存在,删除该记录
DELETE FROM dbA.dbo.infosociety WHERE comid=@comid
Else --不存在时,插入该记录
begin
insert into dbB.dbo.tb_infogegu (comtitle,comcontent,comchannel,comdate,tradetype,stockcode, fromdate)
select top 1 comtitle,comcontent,comchannel,comdate,tradetype,stockcode, fromdate from dbA.dbo.infosociety where comid=@comid order
by comdate
DELETE FROM dbA.dbo.infosociety WHERE comid=@comid
end
close test1
deallocate test1
fetch next from test into @comid,@comtitle
end
DELETE FROM dbA.dbo.infosociety
close test
deallocate test
GO <