oracle 的pl/sql的(树型结构的题)。
例如表:test_func
code codeName superId superName isref
000 北京一小学 0 北京市教育局 0
000001 一年级 000 北京一小学 0
000001101 一年级一班 000001 一年级 1
功能:删除记录(000001 一年级 000 北京一小学 0)时,它下面的子级记录(000001101 一年级一班 000001 一年级 1) 也被删除,同时上面第一条记录的isref变为1,因为它成了叶子结点。 谢谢了
例如表:test_func
code codeName superId superName isref
000 北京一小学 0 北京市教育局 0
000001 一年级 000 北京一小学 0
000001101 一年级一班 000001 一年级 1
功能:删除记录(000001 一年级 000 北京一小学 0)时,它下面的子级记录(000001101 一年级一班 000001 一年级 1) 也被删除,同时上面第一条记录的isref变为1,因为它成了叶子结点。 谢谢了
is
v_count number :=1;
v_superid number;
v_code number;
begin
select superid into v_code from test_func where code=p_code;
while( v_count > 0 )
loop
delete from test_func where code = p_code;
select code,count(1) into v_code,v_count from test_func where superid = p_code group by code;
p_code := v_code;
end loop;
update test_func set isref = 1 where code = v_code;
exception
when others then
dbms_output.put_line('ERROR found in: PRC_DELETE');
rollback;
raise;
end prc_delete;
is
v_count number :=1;
v_code number;
v_code_1 number;
begin
select superid into v_code_1 from test_func where code=p_code;
while( v_count > 0 )
loop
delete from test_func where code = p_code;
select code,count(1) into v_code,v_count from test_func where superid = p_code group by code;
p_code := v_code;
end loop;
update test_func set isref = 1 where code = v_code_1;
exception
when others then
dbms_output.put_line('ERROR found in: PRC_DELETE');
rollback;
raise;
end prc_delete;