Sunday, February 19, 2012

How can I find the matching table from a group of tables? (difficult to explain

Let's say I have a list of IDs called EntryID and each EntryID can belong to ONE table out of a group of six, what is the best way to get a listing of these?

For example:

select r.*
from #Reminders r
left join mytable1 mt1 on (r.EntryID = mt1.EntryID)
left join mytable2 mt2 on (r.EntryID = mt2.EntryID)
left join mytable3 mt3 on (r.EntryID = mt3.EntryID)
left join mytable4 mt4 on (r.EntryID = mt4.EntryID)

As you can see, #Reminders has one field called EntryID (and many rows).

In my example above, only ONE of those tables will actually be able to join but I have no idea which one has the matching EntryID.

What is the best way for me to do this? I want to grab "ReportStatus" from the corresponding "mytable"... (each "mytable" has a ReportStatus column)How come these are distributed between 6 tables?|||Because each table contains different information in addition to the common EntryID field...|||Ok. I will take your word that it is normalised properly.

How many rows in #Reminders? Is there anyway of knowing which of the tables an id belongs to without actually looking in the tables (for example I worked somewhere where they split off the table into 8 tables based on the mod 8 +1 value of each ID)?|||Actually - if there are 6 tables all with the same ID for different entities, do you not have a master table for common attributes which holds all the PKs? How do you enforce the requirement that an id can only appear in one of the six tables?|||Ok. I will take your word that it is normalised properly.

Daaaaaangerous!

No comments:

Post a Comment