Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate a column with hierarchy structure in oracle
    primarykey
    data
    text
    <p>I have a <code>service</code>'s table as below:</p> <pre><code>CREATE TABLE service Id number, Name varchar2(50), ParentId number, PcCode varchar(50); </code></pre> <p>And data of the <code>service</code>'s table:</p> <pre><code>ID NAME PARENTID PCCODE 1 Test1 0 Null 2 Test2 0 Null 3 Test3 1 Null 4 Test4 1 Null 5 Test5 1 Null 6 Test6 2 Null 7 Test7 2 Null 8 Test8 2 Null 9 Test9 2 Null 10 Test10 3 Null 11 Test11 3 Null </code></pre> <p>And I want to update the <code>PcCode</code> column. This is dependent on the <code>Id</code> column and the <code>ParentId</code> column. Example: </p> <pre><code> Id = 1, ParentId = 0 =&gt; PcCode = '001' Id = 2, ParentId = 0 =&gt; PcCode = '002' Id = 3, ParentId = 1 =&gt; PcCode = '001001' Id = 4, ParentId = 1 =&gt; PcCode = '001002' ..... </code></pre> <p><strong>Update</strong></p> <p>I have written 2 sp (2 way) for update <code>PcCode</code>'s column, but I think it's not doing very good.</p> <p><code>Sp1</code>: (done)</p> <pre><code>create or replace procedure service_hierarchy is cursor c_service is select * from service d; cursor c_parents is select k.parentid from service k group by k.parentid order by k.parentid; begin for obj1 in c_service loop update service e set e.pccode = lpad(to_char(rownum), 3, '0') where e.parentid = service.parentid; end loop; for obj2 in c_parents loop update service e set e.pccode = (select f.pccode from service f where f.id = obj2.parentid) || e.pccode where e.parentid = obj2.parentid; end loop; end; </code></pre> <p><code>Sp2</code>: error - It's done. But it returns a result false. And I don't know how to work correctly.</p> <pre><code>CREATE OR REPLACE PROCEDURE service_hierarchy_2 ( parentId NUMBER := 0 ) IS CURSOR v_data IS SELECT * FROM service WHERE parentId = parentId; BEGIN IF (parentId = 0) THEN UPDATE service SET pccode = LPAD(ROWNUM , 3, '0') WHERE parentid = parentId; ELSE FOR data IN v_data LOOP UPDATE service SET pccode = (SELECT 1 FROM service WHERE Id = parentId) || LPAD(ROWNUM , 3, '0') WHERE parentid = parentId; service_hierarchy_2(data.Id); END LOOP; END IF; END; </code></pre> <p>Please help me to resolve <code>sp2</code> or one way be better than 2 sp on top. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload