Just a caveat on using the UDF.
I've found that if the UDF takes too long to process, the SQL statement will go on to the next records and put the UDF out of sync with the return set.
Really important to streamline the UDF in a SQL-statement.
Since this UDF is jumping from table-to-table anyway, I think you would be better off doing a
scan while