oracle - Hierarchical generated tree code on insert -


http://i.stack.imgur.com/wex9w.jpg

on every insert in table, want generate code (1, 1.1, 1.2 ...) based on code parent of object inserting.

my approach this: - make sql function generate code somehow, , called via trigger on every insert in table.

had hard time string parsing, think best solution? thanks.

you find parent object, let's it's code 1.3 , count siblings rows same parent, let's found 2 brothers. new code value 1.3.3. method demands selecting same table on trigger works. , may cause "ora-04091: table name mutating, trigger/function may not see it" when insert several rows @ time. there workarounds avoid this.

another way use view based on query, instead of trigger:

select id, pid, ltrim(sys_connect_by_path(rn, '.'),'.') code   (     select id, pid,             row_number() on (partition level, pid order seq) rn       data connect pid = prior id       start pid null )   connect pid = prior id   start pid null 

sqlfiddle demo

it demands table contains column seq allowing proper ordering of rows.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -