Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

What is the best way to get information from multiple tables?

$
0
0

Hello everyone.  I have a ToDo Table that holds ToDo's that I have set.

This is the sequel that collects the ToDo's...

SELECT ToDo.ID AS TID, ToDo.ContactID, ToDo.ContactType, ToDo.ToDoType, ToDo.FollowupType, ToDo.ToDoDate, ToDo.ToDoPeriod, ToDo.ToDoUrgent AS Urgent, ToDo.DateCreated FROM ToDo WHERE ToDo.StaffID = '" + UserID + "' AND ToDo.CompletedBY IS NULL AND ToDo.ToDoDate < '" + date + "' ORDER By ToDo.ID Desc

However, I also want to get the Contact Name based on the ContactID.  This normally wouldn't be a problem, but depending on the ContactType, the Contact itself could be in any of 4 or 5 different tables (Customers, Leads, Vendors, Labor)...

Here is an example getting the Contact Name only from the Customers Table:

SELECT ToDo.ID AS TID, ToDo.ContactID, ToDo.ContactType, ToDo.ToDoType, ToDo.FollowupType, ToDo.ToDoDate, ToDo.ToDoPeriod, ToDo.ToDoUrgent AS Urgent, ToDo.DateCreated, Customers.LName + ', ' + Customers.FName AS [CName] FROM ToDo LEFT OUTER JOIN Customers ON Customers.ID = ToDo.ContactID WHERE ToDo.StaffID = '" + UserID + "' AND ToDo.CompletedBY IS NULL AND ToDo.ToDoDate < '" + date + "' ORDER By ToDo.ID Desc

So, my question is, what's the best way of doing something like this?  Presently I am getting the ToDo information and then in a foreach I look at the ContactType and make another trip to the database to get the Contact's Name...  This requires a good deal of overhead though, so I am trying to trim this down to something that would be a bit more appropriate (and more efficient).

From there I have no issue creating the nodes, etc., but I'm having a hard time getting all of this in the Datatable.  Should I do something like a Master / Detail DataSet instead?  I'm curious how others on here (Which know much more than I do) would approach something like this.

Thanks Again, I greatly appreciate your help.

-Matt-



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>