📄 新编号查询示例.sql
字号:
/*--查询新编号的示例
要求:
查询出最小的缺号,如果没有,则用最大的id+1
--邹建 2004.12(引用请保留此信息)--*/
--测试数据
create table tb(id char(4))
insert tb select '0002'
--union all select '0001'
--union all select '0003'
--union all select '0004'
union all select '0005'
go
--查询处理1(最小编号为:0001的处理)
select 新id=right(10001+min(id),4)
from(
select id from tb
union all
select '0000'
)a where not exists(
select * from tb where id=right(10001+a.id,4))
--查询处理2(最小编号为表中现有数据的最小编号)
select 新id=right(10001+min(id),4)
from tb a
where not exists(
select * from tb where id=right(10001+a.id,4))
go
--删除测试
drop table tb
/*--测试结果
新id
--------
0001
(所影响的行数为 1 行)
新id
--------
0003
(所影响的行数为 1 行)
--*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -