日期:2014-05-19  浏览次数:20658 次

问个关于id的问题?
假设我使用一种有10个数字的数据类型表示id,从1到10。
       
        每当我新增一条记录时,id自动加1。当加到5时,我删除了等于2的id。然后我继续增加记录,则id从5后继续增加,没有填补2的空白。

        请问id增加到10后还会继续增加,填补2的空白吗?
        如果不能的话,我最后一个记录就添加不进去了,请问该怎么办?

        谢谢各位高手!



------解决方案--------------------
写个函数吧
create function GetId()
returns int
as
begin
declare @table_Pqs table(sid int)
declare @table_Pqh table([id] int)
insert into @table_Pqs
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10


insert into @table_Pqh
select 1 union all
select 2 union all
select 3 union all
select 5 union all
select 7 union all
select 8 union all
select 9 union all
select 10

declare @return_Pqs int
select @return_Pqs=min(sid) from @table_Pqs left join @table_Pqh on sid=[id] where [id] is null

return @return_Pqs

end
--测试
--======================================================
select dbo.GetId()

--结果如下:
-----------
4

(所影响的行数为 1 行)