JP SIngh wrote:
> Hi All
>
> I run a query which gets data from two tables.
What database? Type and version, please.
>
> I store in MainContracts Table
>
> ContractId
> InitiatedBy
>
>
> In the ContractDependents table I store
>
> ContractId
> EmpName
>
> In the empprofile table I store
>
> EmpName
> FirstName
> LastName
> Email
>
> For each contract we have multiple contract dependents.
>
> What I want to do is to write a query to pull the email address of the
> initator of the record and the email of all the contract dependents
> to allow me to send an email to all of them.
>
> At present I doing this.(just the logic not the actual code)
>
> Get contract initiators email
>
> SELECT Contracts.Id, Contracts.initiatedby, EMPProfile.EmpName,
> EMPProfile.email
> FROM Contracts LEFT JOIN EMPProfile ON Contracts.initiatedby =
> EMPProfile.EmpName
> WHERE (((Contracts.Id)=54207));
Hmm, all the parentheses makes this look like the sql created by the Access
Query Builder, so I will assume you are using Access. Please don't make us
guess.
>
> Send email to the contract initiator
>
> Get the list of all contract dependents and thier email addresses
>
> SELECT Contractdependents.contractnumber, Contractdependents.EMPName,
> EMPProfile.EmpName, EMPProfile.email
> FROM Contractdependents LEFT JOIN EMPProfile ON
> Contractdependents.EMPName = EMPProfile.EmpName
> WHERE (((Contractdependents.contractnumber)=54207));
>
> Loop through the above recordset and send email to each dependent.
>
> I feel opening two recordsets are slow and not efficient.
>
> Am I correct? is there a better way of doing this.
>
> Regards
> Jas
It sounds like you need a union query. It could be as simple as this:
SELECT c.Id, c.initiatedby, e.EmpName,e.email
FROM Contracts c LEFT JOIN EMPProfile e ON c.initiatedby =
e.EmpName
WHERE c.Id=54207
UNION
SELECT c.contractnumber, c.EMPName,e.EmpName, e.email
FROM Contractdependents c LEFT JOIN EMPProfile e ON c.EMPName =
e.EmpName
WHERE c.contractnumber=54207
Is the email to the initiator different from the one sent to the dependants?
If so, you will need to include
SELECT 'Initiator' As Source,
c.Id, c.initiatedby, e.EmpName,e.email
FROM Contracts c LEFT JOIN EMPProfile e ON c.initiatedby =
e.EmpName
WHERE c.Id=54207
UNION
SELECT 'Dependant' As Source,
c.contractnumber, c.EMPName,e.EmpName, e.email
FROM Contractdependents c LEFT JOIN EMPProfile e ON c.EMPName =
e.EmpName
WHERE c.contractnumber=54207
ORDER BY Source DESC
HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"