请教各位大佬执行插入语句时,如何生成 001 001001 001002 002 002001 002002这种编号,非常感谢!

lhy501208 2018-07-09 11:04:29
001
001001
001001001
001001002
001002
002
002001
...全文
1361 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
直接定义为字符串,然后在JS里面拼接起来,一般来说项目里面是会限定编号长度的,所以还要加一个字符串长度判断和截取
roc_hello 2018-07-10
  • 打赏
  • 举报
回复
直接定义为字符串吧!
星先 2018-07-10
  • 打赏
  • 举报
回复

insert into organization
select top 1 (select max(id)+1 from organization)
,(select isnull(max(org_no),'') from organization where id=@pid)+right('000'+convert(varchar,max(
convert(int,
right(org_no,
len(org_no)-len(select max(org_no) from organization where id=@pid)
)
)
)+1
),
len(select max(org_no) from organization where id=@pid)+3
)
,@orgname
,@pid
from organization
where parent_id=@pid
yaiger 2018-07-10
  • 打赏
  • 举报
回复
select to_char(max(org_no)+1, rpad('fm', length(org_no) + 2, '0')) orgNo from uap_organization p where p.parent_id = '1'
  • 打赏
  • 举报
回复
你这个分组标识都没得吗。。
qinshiling007 2018-07-10
  • 打赏
  • 举报
回复
拼接字符串用||
lhy501208 2018-07-09
  • 打赏
  • 举报
回复
就是想在执行插入操作时,自动生成是这种树状编码形式的组织编号
yaiger 2018-07-09
  • 打赏
  • 举报
回复
这是树状编码吧
获取父节点的编码,然后在拼当前节点的编码就好了,不太明白你的意思。

select to_char(1, 'fm000') from dual; 这个语句生成的结果就是001,自己看看怎么弄吧
lhy501208 2018-07-09
  • 打赏
  • 举报
回复
select to_char(max(org_no)+1, rpad('fm', length(org_no), '0')) orgNo from uap_organization p where p.parent_id = '1' group by org_no
结果:1002 1003 前面的0丢失了

yaiger 2018-07-09
  • 打赏
  • 举报
回复
这个写法更好,可以不用管在哪个层级
select to_char(max(org_no)+1, rpad('fm', length(org_no), '0')) orgNo from uap_organization p where p.parent_id = '1'
yaiger 2018-07-09
  • 打赏
  • 举报
回复
select to_char(max(org_no)+1, 'fm000000') orgNo from uap_organization p where p.parent_id = '1'
lhy501208 2018-07-09
  • 打赏
  • 举报
回复
select max(org_no)+1 orgNo from uap_organization p where p.parent_id = '1'
查询结果1003
yaiger 2018-07-09
  • 打赏
  • 举报
回复
你怎么转换的?把代码贴一下
lhy501208 2018-07-09
  • 打赏
  • 举报
回复
create table organization
(
id INTEGER not null,
org_code VARCHAR2(32) default NULL,
org_name VARCHAR2(128) default NULL,
org_no VARCHAR2(32) default NULL,
parent_id INTEGER default NULL,
constraint PK_ORGANIZATION primary key (id)
);

这是表结构,转成整形再转字符型好像也是0丢了
yaiger 2018-07-09
  • 打赏
  • 举报
回复
如果表里面定义的节点字段是字符型的,是不会丢0

你想的是怎么获取下个节点编号吧?
可以考虑抓取当前父节点下的最大子节点编码,转换成整型+1后再转换成字符型

你可以把表结构贴一下,这样可以回答得更清晰
lhy501208 2018-07-09
  • 打赏
  • 举报
回复
试过,不过0没有补齐,想插入 001 001001 001002 001002这种,还是过取最大子节点编号再加1,也是中间的0丢了001003变成1003
yaiger 2018-07-09
  • 打赏
  • 举报
回复
select parent_id || child_id from dual;
或者
直接用变量
V_ID := parent_id || child_id;
lhy501208 2018-07-09
  • 打赏
  • 举报
回复
我现在就是不知道怎么拼接。。。。
yaiger 2018-07-09
  • 打赏
  • 举报
回复
拼接字符串用||
lhy501208 2018-07-09
  • 打赏
  • 举报
回复
就是不知道怎么拼接子节点编号,父节点编号我能获取
加载更多回复(4)

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧