hello! i am adding in contact management features to an application. Each Contact can have multiple roles in her origanization. there are two tables involved. (a third table is a lookup/results table, but that is not important to show).
CREATE TABLE Contacts (
ContactID int ,
ContactName varchar(200) ,
ContactEmail varchar(200) ,
ContactPhone varchar(200)
)
CREATE TABLE ContactRoles (
ContactRoleID int , -- just an arbitrary auto-number
ContactID int , -- related to table Contacts
RoleID int -- from the lookup table
)
so as you see, if one Contact serves three roles in the organization (e.g., Advertising, Marketing, and Sales) then there will be one row in the Contacts table to store her name, etc., and three related rows in the ContactRoles table to associate each of these roles with her ContactID. pretty simple so far.
so when it comes time to display all of the Contacts in the organization, i would like to build an HTML table with one row for each Contact, and have the last cell in the show all of her roles, as a comma-delimited list or whatver. but i want one row per contact, not multiple rows.
i can do this of course by generating multiple, nested recordsets: i.e., populate the first recordset with all Contacts, then loop through it one row at a time, generating another recordset of Roles for this contact, and hence build the table one row at a time. i do not like this approach as it means if my table is going to list 100 Contacts, it would require 101 recordsets. I would like to do this with one recordset, but am not sure how. I would like to avoid using Cursors if possible. So after much ado, my question is: how can i retrieve the data needed for this task in the most elegant, efficient way possible? my platform is ASP/ADO using SQLServer 2000.
please forgive the cross-post to the related SQL Server group. and thanks for your replies!
CREATE TABLE Contacts (
ContactID int ,
ContactName varchar(200) ,
ContactEmail varchar(200) ,
ContactPhone varchar(200)
)
CREATE TABLE ContactRoles (
ContactRoleID int , -- just an arbitrary auto-number
ContactID int , -- related to table Contacts
RoleID int -- from the lookup table
)
so as you see, if one Contact serves three roles in the organization (e.g., Advertising, Marketing, and Sales) then there will be one row in the Contacts table to store her name, etc., and three related rows in the ContactRoles table to associate each of these roles with her ContactID. pretty simple so far.
so when it comes time to display all of the Contacts in the organization, i would like to build an HTML table with one row for each Contact, and have the last cell in the show all of her roles, as a comma-delimited list or whatver. but i want one row per contact, not multiple rows.
i can do this of course by generating multiple, nested recordsets: i.e., populate the first recordset with all Contacts, then loop through it one row at a time, generating another recordset of Roles for this contact, and hence build the table one row at a time. i do not like this approach as it means if my table is going to list 100 Contacts, it would require 101 recordsets. I would like to do this with one recordset, but am not sure how. I would like to avoid using Cursors if possible. So after much ado, my question is: how can i retrieve the data needed for this task in the most elegant, efficient way possible? my platform is ASP/ADO using SQLServer 2000.
please forgive the cross-post to the related SQL Server group. and thanks for your replies!