我有一个SQL查询,我正在测试,并运行如下,但我注意到它似乎每次都返回不同的数据,然后我意识到它甚至返回一个当我检查它是否已经工作时不同数量的行!我已经多次运行了它,最终的select语句将返回25-32行之间的某个地方,但是这个变化怎么样?SQL更新,相同的查询,每次不同的结果
我正在使用begin tran
和rollback tran
来处理相同的数据,并不认为这是问题所在。任何人都可以发现我做错了什么?
它在一个表(#AddressToDeleteMasterOfLesserId
)上工作,它是Id的成对,并在客户地址上设置一个标志(IsPrimaryAddress)(如果它存在于表中并且它的对具有设置的标志)。 #AddressToDeleteMasterOfLesserId
已被定义,不会更改。
begin tran t1
select CustomerAddress.IsPrimaryAddress, p1.[Id that is master],p1.[Id to delete], c2.IsPrimaryAddress
FROM CustomerAddress
join #AddressToDeleteMasterOfLesserId p1 on CustomerAddress.Id=p1.[Id that is master]
join CustomerAddress c2 on p1.[Id to delete]=c2.Id
order by [Id that is master]
--Update primary address
UPDATE CustomerAddress
SET IsPrimaryAddress = CASE WHEN c2.IsPrimaryAddress=1 THEN 1 ELSE 0 END
FROM CustomerAddress
join #AddressToDeleteMasterOfLesserId p1 on CustomerAddress.Id=p1.[Id that is master]
join CustomerAddress c2 on p1.[Id to delete]=c2.Id
select CustomerAddress.IsPrimaryAddress, p1.[Id that is master],p1.[Id to delete], c2.IsPrimaryAddress
FROM CustomerAddress
join #AddressToDeleteMasterOfLesserId p1 on CustomerAddress.Id=p1.[Id that is master]
join CustomerAddress c2 on p1.[Id to delete]=c2.Id
where CustomerAddress.IsPrimaryAddress=0
and c2.IsPrimaryAddress=1
order by [Id that is master]
rollback tran t1
OF COURSE !!!!现在我再看一遍,这是非常有意义的。我会问另一个问题,我该如何解决这个问题,但是你知道是否有办法让备选案例“无所事事”,设置为自身不起作用,因为它会在第一次读取时出错,从而给出错误的答案 – 2012-03-21 20:46:56