日期:2014-05-18  浏览次数:20482 次

求教:数据插入删除语句
我有两个数据库,分别是:A库、B库。现在我想把A库中的a1表的数据插入到B库中的b1表中,并删除(想法是插入一条删除一条。a1表是有数据实时写入的。)a1与b1表的结构是完全想同的。

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

use test
go
SET NOCOUNT ON
if OBJECT_ID('dbo.tb_A','U') is not null
    drop table dbo.tb_A
create table dbo.tb_A
(
    id int not null
);
GO
if OBJECT_ID('dbo.tb_B','U') is not null
    drop table dbo.tb_B
create table dbo.tb_B
(
    id int not null
);    
GO    

INSERT INTO dbo.tb_A
SELECT 1
UNION ALL
SELECT 2
;
GO

SELECT *
FROM dbo.tb_A;
GO

DELETE 
FROM dbo.tb_A
    OUTPUT deleted.*
    INTO dbo.tb_B
WHERE id = 1;
GO

SELECT *
FROM dbo.tb_A;

SELECT *
FROM dbo.tb_B;

------解决方案--------------------
create table #t1(col1 varchar(10),col2 varchar(10),col3 varchar(10))
create table #t2(col1 varchar(10),col2 varchar(10),col3 varchar(10))

while exists(select top 1 0 From #t1)
begin
delete top (100) from #t1 output deleted.* into #t2
waitfor delay '00:00:00:300'
end


--用循环处理