oracle 自关联的表 更新所有上级
发布网友
发布时间:2022-05-02 01:14
我来回答
共1个回答
热心网友
时间:2022-04-10 20:12
--准备一些数据
drop table tb_menu purge;
create table tb_menu(
id number not null,
name varchar2(20),
pid number,
grp number
);
--父菜单
insert into tb_menu(id, name, pid) values(1, '父菜单1',null);
insert into tb_menu(id, name, pid) values(2, '父菜单2',null);
insert into tb_menu(id, name, pid) values(3, '父菜单3',null);
insert into tb_menu(id, name, pid) values(4, '父菜单4',null);
insert into tb_menu(id, name, pid) values(5, '父菜单5',null);
--一级菜单
insert into tb_menu(id, name, pid) values(6, '一级菜单6',1);
insert into tb_menu(id, name, pid) values(7, '一级菜单7',1);
insert into tb_menu(id, name, pid) values(8, '一级菜单8',1);
insert into tb_menu(id, name, pid) values(9, '一级菜单9',2);
insert into tb_menu(id, name, pid) values(10, '一级菜单10',2);
insert into tb_menu(id, name, pid) values(11, '一级菜单11',2);
insert into tb_menu(id, name, pid) values(12, '一级菜单12',3);
insert into tb_menu(id, name, pid) values(13, '一级菜单13',3);
insert into tb_menu(id, name, pid) values(14, '一级菜单14',3);
insert into tb_menu(id, name, pid) values(15, '一级菜单15',4);
insert into tb_menu(id, name, pid) values(16, '一级菜单16',4);
insert into tb_menu(id, name, pid) values(17, '一级菜单17',4);
insert into tb_menu(id, name, pid) values(18, '一级菜单18',5);
insert into tb_menu(id, name, pid) values(19, '一级菜单19',5);
insert into tb_menu(id, name, pid) values(20, '一级菜单20',5);
--二级菜单
insert into tb_menu(id, name, pid) values(21, '二级菜单21',6);
insert into tb_menu(id, name, pid) values(22, '二级菜单22',6);
insert into tb_menu(id, name, pid) values(23, '二级菜单23',7);
insert into tb_menu(id, name, pid) values(24, '二级菜单24',7);
insert into tb_menu(id, name, pid) values(25, '二级菜单25',8);
insert into tb_menu(id, name, pid) values(26, '二级菜单26',9);
insert into tb_menu(id, name, pid) values(27, '二级菜单27',10);
insert into tb_menu(id, name, pid) values(28, '二级菜单28',11);
insert into tb_menu(id, name, pid) values(29, '二级菜单29',12);
insert into tb_menu(id, name, pid) values(30, '二级菜单30',13);
insert into tb_menu(id, name, pid) values(31, '二级菜单31',14);
insert into tb_menu(id, name, pid) values(32, '二级菜单32',15);
insert into tb_menu(id, name, pid) values(33, '二级菜单33',16);
insert into tb_menu(id, name, pid) values(34, '二级菜单34',17);
insert into tb_menu(id, name, pid) values(35, '二级菜单35',18);
insert into tb_menu(id, name, pid) values(36, '二级菜单36',19);
insert into tb_menu(id, name, pid) values(37, '二级菜单37',20);
--*菜单
insert into tb_menu(id, name, pid) values(38, '*菜单38',21);
insert into tb_menu(id, name, pid) values(39, '*菜单39',22);
insert into tb_menu(id, name, pid) values(40, '*菜单40',23);
insert into tb_menu(id, name, pid) values(41, '*菜单41',24);
insert into tb_menu(id, name, pid) values(42, '*菜单42',25);
insert into tb_menu(id, name, pid) values(43, '*菜单43',26);
insert into tb_menu(id, name, pid) values(44, '*菜单44',27);
insert into tb_menu(id, name, pid) values(45, '*菜单45',28);
insert into tb_menu(id, name, pid) values(46, '*菜单46',28);
insert into tb_menu(id, name, pid) values(47, '*菜单47',29);
insert into tb_menu(id, name, pid) values(48, '*菜单48',30);
insert into tb_menu(id, name, pid) values(49, '*菜单49',31);
insert into tb_menu(id, name, pid) values(50, '*菜单50',31);
commit;
查询*菜单50的各级父菜单
select id from tb_menu m start with m.id=50 connect by prior m.pid=m.id;
把这些相关的菜单的grp更新为99
update tb_menu set grp=99 where id in (
select id from tb_menu m start with m.id=50 connect by prior m.pid=m.id);