第1个回答 2012-06-10
前提条件 B数据条数大于A
declare @num int,@count int
Set @num=1;
SET @count=(select MAX(ID) from A);--获取A最大ID,ID是自动增长
PRINT @COUNT
while(@num<=@count)
BEGIN
--更新数据
update A SET NAME=(select Name from B where ID=@num) where ID=@num
SET @num=@num+1
PRINT @num
END
print '更新后的表:'
SELECT * FROM A
--2插入A表没有是数据:
insert into A(NAME)(select NAME from B WHERE ID not IN (SELECT TOP(@count) ID FROM B))
PRINT '插入后的表:'
SELECT * FROM A