close
DECLARE @NewID bigint = 0
DECLARE @table_temp TABLE(tb_id bigint)
DECLARE @max INT, @min INT
-- 取最小值
SET @min = (SELECT MIN(tb_id) FROM dbo.table01)
-- 取最大值
SET @max = (SELECT MAX(tb_id) FROM dbo.table01)
-- 跑迴圈
WHILE @min <= @max
BEGIN
-- 存入暫存表
INSERT INTO @table_temp ( tb_id )
VALUES ( @min )
-- 最小的值加1
SET @min = @min + 1
END
-- 找出 不連續編號
select * from (
SELECT tb_id FROM @table_temp
EXCEPT
SELECT tb_id FROM dbo.table01
) table1
-- 判斷 讀不到值,最大的加1
IF @NewID is NULL set @NewID = @max + 1
select @NewID
文章標籤
全站熱搜
留言列表