插入和删除 - 按顺序运行

时间:2015-10-08 15:05:54

标签: sql sql-server

是否可以一个接一个地运行多个语句,但只能在前一个语句完成运行时执行下一个语句?

我有一条声明,它将数据从一个数据库复制到一个存档数据中。 然后它会从" live"删除数据。 db一旦被复制。

如果我单独执行以下查询的每个部分,它可以正常工作。 如果我将整个事情作为一个语句执行,那么它就会出错,大概是因为它在完成复制数据之前试图从故障中删除它。 INSERT上述陈述。

--- Copies data from Faults into Archive DB
INSERT INTO Faults 
SELECT *
FROM OriginalDB.dbo.Faults
where Flastactiondate < '2012-09-30'

-- Deletes archived faults from live db
Delete  from OriginalDB.dbo.Faults
where OriginalDB.dbo.Faults.Faultid in (select FaultID from ArchiveDB.dbo.Faults)



-- Copies data from actions into archiveTB
Insert Into Actions
select * 
from OriginalDB.dbo.ACTIONS
where OriginalDB.dbo.ACTIONS.Faultid in (select FaultID from ArchiveDB.dbo.Faults)

-- Deletes archived actions form live db
delete 
from OriginalDB.dbo.ACTIONS
where OriginalDB.dbo.ACTIONS.Faultid in (select FaultID from ArchiveDB.dbo.Faults)

理想情况下,我希望能够启动它,并让它在一夜之间运行,而无需手动运行每个语句。

1 个答案:

答案 0 :(得分:1)

我之前评论的内容:

--- Copies data from Faults into Archive DB
INSERT INTO Faults SELECT *
FROM OriginalDB.dbo.Faults
where Flastactiondate < '2012-09-30'

GO

-- Deletes archived faults from live db
Delete  from OriginalDB.dbo.Faults
where OriginalDB.dbo.Faults.Faultid in (select FaultID from        ArchiveDB.dbo.Faults)

GO

-- Copies data from actions into archiveTB
Insert Into Actions select * 
from OriginalDB.dbo.ACTIONS
where OriginalDB.dbo.ACTIONS.Faultid in (select FaultID from    ArchiveDB.dbo.Faults)

GO

-- Deletes archived actions form live db
delete from OriginalDB.dbo.ACTIONS
where OriginalDB.dbo.ACTIONS.Faultid in (select FaultID from ArchiveDB.dbo.Faults)

GO