EDIT
Sorry for the half post :(
It seems I cannot use a column from the parent query in a sub query. How can I refactor this query to get what I need?
dbo.func_getRelatedAcnts
returns a table of related accounts (all children from a given account). It goes up to the top level parent account of a given account and then selects all child accounts from there. This gives me a table of all the parent, sibling, and child account ids.
Events has a foreign key to an Account and Profiles has a foreign key to accounts.
Registrations have a foreign profile key and event key. There can be multiple regs per profile. I need to find all the regs that are associated with profiles that are in accounts that are not in any way related hierarchically to the event the registration is in.
The problem is that I want to use the profile account key of profile related to the reg in the parent query to dynamically grab the table in the subquery.
SELECT COUNT(r.reg_id)
FROM registrations r
JOIN profiles p ON (r.reg_frn_pro_id = p.pro_id)
JOIN events e ON (r.reg_frn_evt_id = e.evt_id)
WHERE evt_frn_acnt_id NOT IN
(SELECT * FROM dbo.func_getRelatedAcnts(p.pro_frn_acnt_id))
My error:
pro_frn_acnt_id is not a recognized table hints option. If it is intended as a parameter to a table-valued function, ensure that your database compatibility mode is set to 90.