这是一个简单的设计,简化了“碰撞”的处理。您只需要更新一行即可使用此方法:
UserEmailAddresses Table
------------------------
YourPKHere <whatever you have, identity?>
UserId <whatever you have>
EmailAddressId <whatever you have>
DisplaySeq INT
LastChgDate datetime
SELECT * FROM UserEmailAddresses ORDER BY DisplaySeq ASC, LastChgDate DESC
编辑示例代码
DECLARE @UserEmailAddresses table
(
YourPKHere int identity(1,1) primary key
,UserId int
,EmailAddressId varchar(100)
,DisplaySeq INT
,LastChgDate datetime
)
--existing data
INSERT INTO @UserEmailAddresses values (1,'one@one.com',1,'1/1/2009')
INSERT INTO @UserEmailAddresses values (1,'two@two.com',2,'2/2/2009')
INSERT INTO @UserEmailAddresses values (1,'three@three.com',3,'3/3/2009')
INSERT INTO @UserEmailAddresses values (2,'one2@one2.com',1,'1/1/2009')
INSERT INTO @UserEmailAddresses values (2,'two2@two2.com',2,'2/2/2009')
--application updates one row, no locking or blocking
update @UserEmailAddresses set DisplaySeq=1,LastChgDate=getdate() where UserId=1 and EmailAddressId='two@two.com' --could say WHERE YourPKHere=n, but you don't give your complete table schema
--display the emails in proper order, with displayable continuous row numbers
SELECT
*, ROW_NUMBER() over(partition by UserId order by DisplaySeq ASC,LastChgDate DESC) AS ActualDuisplaySeq
FROM @UserEmailAddresses
WHERE UserId=1
--display the e-mails in proper order
SELECT * FROM @UserEmailAddresses Where UserId=1 ORDER BY DisplaySeq ASC, LastChgDate DESC