I have a table Action with a (part of the) structure like this:
Action:
ActionTypeID,
ActionConnectionID
…
ActionTypeID refers to types 1-5 which correspond to different tables (1=Pro, 2=Project etc.)
ActionConnectionID is the primaryKey in the corresponding table;
ie. ActionTypeID=1,
ActionConnectionID=43 -> would point
to Pro.ProID=43 and ActionTypeID=2,
ActionConnectionID=233 -> would point
to Project.ProjectID=233
Is there a way to ‘dynamically join the different tables depending on the value in the ActionTypeID column?
ie. for records with the ActionTypeID=1 this would be:
Select Action.*
From Action Left Join Pro On Action.ActionConnectionID=Pro.ProID
for records with the ActionTypeID=2 this would be:
Select Action.*
From Action Left Join Project On Action.ActionConnectionID=Project.ProjectID
etc.
If this is not possible to accomplish in one query I will have to loop over all the possible ActionTypes and perform the query and then afterwards join the data in one query again – that would be possible, but doesnt sound like the most efficient way 🙂
Something like this should do:
If that doesn’t work for either try using dynamic sql which is a bad solution or properly normalize your data.