?? 新編號查詢示例.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 + -