可以使用xlock锁来控制并发操作时生成的PlanID不相同:
create proc spMaxID
as
begin transaction /*启动事务*/
declare @MaxID int
--使用holdlock使锁保持到事务结束,xlock保证事务期间禁止其他用户读取,防止生成相同的PlanID.
select @MaxID = isnull(max(PlanID),0)+1 as PlanID from T_Plan_Main with (holdlock,xlock)
--插入最大值
insert into T_Plan_Main(PlanID) values(@MaxID)
commit transaction /*提交事务,同时自动解锁,以保证其他用户生成新的PlanID*/
GO