一級(2位),二級以后占用(3位),比如一級:01或02,二級:01001或01003,那三級編號就是01001001或01111112,我就寫了一個存儲過程【個人測試沒問題,可以參考一下】獲取即將要添加的編號(模擬場景:選擇權限的樹狀圖節點添加子集節點,需要計算出子集節點的將要插入的id)
ALTER PROCEDURE [dbo].[GetMaxId]
(
?? @tablename nvarchar(100),--表名
?? @idcol nvarchar(100),--列名
?? @parent nvarchar(100),--父級編號
?? @jb int,--級別
?? @ids nvarchar(50) output--最終的id
)
AS
BEGIN
declare @str nvarchar(2000)
set @str=''
?? --一級添加
?? if @jb=1
???? begin
????? set @str=@str+'select @ids=cast(max(
'+@idcol+'
) as int)+1 from
'+@tablename+'
where len(
'+@idcol+')<=2
'
????? exec sp_executesql @str,N'@ids nvarchar(50) output',@ids output
???? if(len(@ids) is null)
????????? set @ids='01'
???? else IF(len(@ids)<=2)
?????? set @ids='0'+@ids
???? end
?? else
??? begin
?????? set @str=@str+'select @ids=CAST(max(RIGHT(
'+@idcol+',3))as
int)+1 from
'+@tablename+'
where len(
'+@idcol+')=2+('+CAST
(
@jb as nvarchar(20)) +'-1)*3 and
'+@idcol+'
like
'''+@parent+'%'''
?????? exec sp_executesql @str,N'@ids nvarchar(50) output',@ids output
??????? IF(len(@ids)=1)
???????? set @ids='00'+@ids
?????? else IF(len(@ids)=2)
???????? set @ids='0'+@ids
????? else if(len(@ids) is null)
?????????? set @ids='001'
?????? IF @parent<>'' and @parent is not null
??????? SET @ids=@parent+@ids
??? end
END
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
