set nocount on
declare @testtable table (
id int identity,
OwnerDEipUserID int,
SrcUserID int,
DEipUserID int,
SrcUserOwnerID int
)
while (select COUNT (*) FROM @testtable) < 10000 BEGIN
INSERT INTO @testtable
select -1, @@IDENTITY, @@IDENTITY % 7, @@IDENTITY % 13
end
select * from @testtable
while (select COUNT(*) FROM @testtable WHERE OwnerDEipUserID =-1) > 0 BEGIN
UPDATE top (100) b
SET b.OwnerDEipUserID = a.DEipUserID
from @testtable a
join @testtable b
on a.SrcUserID = b.SrcUserOwnerID
where b.OwnerDEipUserID = -1
select COUNT(*) from @testtable WHERE OwnerDEipUserID =-1
end
select * from @testtable
|