Sorry about the rubbish question title. I have a table SET_DEFINITIONS like this:
SETKEY NOT NULL NUMBER(38)
SETENTRY NOT NULL NUMBER(38)
where the idea is that the rows define sets of numbers. For example the table could contain rows:
1 2
1 4
2 1
2 2
which would mean set 1 is {2,4} and set 2 is {1,2}. I want to write a function
function selectOrInsertSet(table of number(38) numbers) return number(38)
which will return the key of a set with the same members as the passed in table (or create such a set if it doesn’t exist). What’s a good way to do this in PL/SQL?
EDIT: the solution I’m currently working on goes like this (and I’m not sure it’ll work):
- select all keys that have the first element into some collection c
- refine the collection c by intersecting with successive sets of keys that contain the other elements
You can use a full outer join between each set and the collection of numbers to see if they are the same. This function does that:
This returns the setkey if found, else null.
I haven’t implemented the part about creating a new set if none is found, but that should be easy enough. I don’t personally like functions that have side effects (in this case, inserting rows into a table).