请试试这个,对你有帮助
可以通过使用存储过程和函数,我创建它有一个额外的列,您可以在表中创建 FLAG 名称和列,所有字段值都应该是 0 然后它可以工作
create Procedure proc_randomprimarykeynumber
as
declare @Primarykeyid int
select top 1
@Primarykeyid = u.ID
from
StateAbbreviation u
left join
StateAbbreviation v on u.ID = v.ID + 1
where
v.flag = 1
if(@Primarykeyid is null )
begin
UPDATE StateAbbreviation
SET flag = 0
UPDATE StateAbbreviation
SET flag = 1
WHERE ID IN (SELECT TOP 1 ID
FROM dbo.StateAbbreviation)
END
ELSE
BEGIN
UPDATE StateAbbreviation
SET flag = 0
UPDATE StateAbbreviation
SET flag = 1
WHERE ID IN (@Primarykeyid)
END
SET @Primarykeyid = 1
SELECT TOP 1
ID, Abbreviation
FROM
StateAbbreviation
WHERE
flag = 1
它是在存储过程中创建的,运行它并获取串行主键
exec proc_randomprimarykeynumber
感谢和尊重