求教:数据插入删除语句
我有两个数据库,分别是:A库、B库。现在我想把A库中的a1表的数据插入到B库中的b1表中,并删除(想法是插入一条删除一条。a1表是有数据实时写入的。)a1与b1表的结构是完全想同的。
[解决办法]
- SQL code
use testgoSET NOCOUNT ONif OBJECT_ID('dbo.tb_A','U') is not null drop table dbo.tb_Acreate table dbo.tb_A( id int not null);GOif OBJECT_ID('dbo.tb_B','U') is not null drop table dbo.tb_Bcreate table dbo.tb_B( id int not null); GO INSERT INTO dbo.tb_ASELECT 1UNION ALLSELECT 2;GOSELECT *FROM dbo.tb_A;GODELETE FROM dbo.tb_A OUTPUT deleted.* INTO dbo.tb_BWHERE id = 1;GOSELECT *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
--用循环处理