Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query multiple DBs

Status
Not open for further replies.

tina2

IS-IT--Management
Joined
May 1, 2001
Messages
169
Location
US
I have two databases with identical fields. Is there a way to reference two datasources for one query? How would I go about that?

I am using CF5
Thanks,
Kristine
 
I believe you can use queries of queries to do this.

Theres a Ben Forta example somewhere of how to do this if I find it I'll post the link.

Kola

 
Thanks Kola.

I didn't find it on Ben Forta's site, but I found quite a bit in the Macromedia forums. I understand the concept, but I'm having trouble with the following query. I have a WebUser and WebUserLookup table in the main database, and I am trying to pull the corresponding policy information from the second DB (ZenithWeb2). CF is not able to evaluate PolicyID from my Q of Qs. I'm thinking I need to do an inner join in the third query, but I'm not sure how...

<CFQUERY NAME=&quot;qGetPolicy&quot; DATASOURCE=&quot;ZenithWeb&quot;>
SELECT WebUsers.UserID, WebUserLookup.UserID, WebUserLookup.PolicyID, Policy.PolicyID, Policy.name
FROM (WebUsers INNER JOIN WebUserLookup ON WebUsers.UserID = WebUserLookup.UserID) INNER JOIN Policy ON [WebUserLookup.PolicyID = Policy.PolicyID] AS PolicyID
WHERE WebUsers.UserID = #session.UserID#
ORDER BY Policy.name
</CFQUERY>
<CFQUERY NAME=&quot;qGetPolicy2&quot; DATASOURCE=&quot;ZenithWeb2&quot;>
SELECT Policy.PolicyID, Policy.name
FROM Policy
</CFQUERY>
<cfquery NAME=&quot;qOfQgetPolicy&quot; dbtype=&quot;query&quot;>
SELECT qGetPolicy.PolicyID, qGetPolicy.UserID, qGetPolicy.Name, qGetPolicy2.PolicyID, qGetPolicy2.Name
FROM qGetPolicy, qGetPolicy2
WHERE qGetPolicy.PolicyID = qGetPolicy2.PolicyID AS PolicyID
</cfquery>

Does anyone have any ideas?
Kristine
 
the problem lies i think in thr where statement of your final query(Q of Q's)

WHERE qGetPolicy.PolicyID = qGetPolicy2.PolicyID AS PolicyID

you need to assign an alias to a field before this point in the select statement.

so your q of q's will read:

<cfquery NAME=&quot;qOfQgetPolicy&quot; dbtype=&quot;query&quot;>
SELECT qGetPolicy.PolicyID, qGetPolicy.UserID, qGetPolicy.Name, qGetPolicy2.PolicyID AS QPOLICYID, qGetPolicy2.Name
FROM qGetPolicy, qGetPolicy2
WHERE qGetPolicy.PolicyID = qGetPolicy2.QPOLICYID
</cfquery>

good luck

tony
 
Also I'm no SQL expert but does the first query work with this line:

FROM (WebUsers INNER JOIN WebUserLookup ON WebUsers.UserID = WebUserLookup.UserID)
DO you still need the:
= WebUserLookup.UserID

Let us know how you get on!
 
Thanks Ya'll!
pigsie, the Inner Join does work when I run the querey on its own. (I'm no SQL expert either, but I used the Access query builder ;-))

Tony, I used your suggestion, but I assigned the alias in the second query:
<CFQUERY NAME=&quot;qGetPolicy2&quot; DATASOURCE=&quot;ZenithWeb2&quot;>
SELECT Policy.PolicyID AS QPOLICYID, Policy.name
FROM Policy
</CFQUERY>

and called it in the Q of Qs, which didn't throw an error, but I'm back to the problem of evaluating &quot;qOfqGetPolicy.PolicyID&quot;

The first place I call the query is to polulate a dropdown list with the policy names:
<option value=&quot;#valuelist(qOfqGetPolicy.PolicyID)#&quot;>All</option> Which works like a charm when I use the original query, but throws an error when I try to pull it from the Q of Qs...

I also got rid of the []AS PolicyID in the statement ON [WebUserLookup.PolicyID = Policy.PolicyID] AS PolicyID
in the first query, which was a lame attempt at passing the ID, which did not work.

Any ideas?
Kristine
 
is this what you q of q's now looks like ?

<cfquery NAME=&quot;qOfQgetPolicy&quot; dbtype=&quot;query&quot;>
SELECT qGetPolicy.PolicyID, qGetPolicy.UserID, qGetPolicy.Name, qGetPolicy2.PolicyID, qGetPolicy2.Name
FROM qGetPolicy, qGetPolicy2
WHERE qGetPolicy.PolicyID = qGetPolicy2.QPolicyID
</cfquery>

if so then you have got two fields that are named the same in your select statement (policyID). change the qGetPolicy2.PolicyID to qGetPolicy.QPolicyID

CF doesn't know which one you are referring to in your output statement so just outputs the first one that it comes to !

if this doesn't help can you post up the three queries again
 
There lies the problem!
ZenithWeb and ZenithWeb2 have identical feilds. I need to get the PolicyID and Policy.Name from both DBs and display them in one page.

<---! first query pulls the userID from the users table,and the userlookup table, which contains corresponding policyIDs from both DBs --->

<CFQUERY NAME=&quot;qGetPolicy&quot; DATASOURCE=&quot;ZenithWeb&quot;>
SELECT WebUsers.UserID, WebUserLookup.UserID, WebUserLookup.PolicyID, Policy.PolicyID, Policy.name
FROM (WebUsers INNER JOIN WebUserLookup ON WebUsers.UserID = WebUserLookup.UserID) INNER JOIN Policy ON WebUserLookup.PolicyID = Policy.PolicyID
WHERE WebUsers.UserID = #session.UserID#
ORDER BY Policy.name

<---! Second query pulls PolicyID and Policy Name from ZenithWeb2 --->

</CFQUERY>
<CFQUERY NAME=&quot;qGetPolicy2&quot; DATASOURCE=&quot;ZenithWeb2&quot;>
SELECT Policy.PolicyID AS QPOLICYID, Policy.name
FROM Policy
</CFQUERY>

<---! Qof Qs needs to get the Policy.Name and PolicyID from both DBs --->

<cfquery NAME=&quot;qOfQgetPolicy&quot; dbtype=&quot;query&quot;>
SELECT qGetPolicy.PolicyID, qGetPolicy.UserID, qGetPolicy.Name, qGetPolicy2.QPOLICYID, qGetPolicy2.Name
FROM qGetPolicy, qGetPolicy2
WHERE qGetPolicy.PolicyID = qGetPolicy2.QPOLICYID
</cfquery>

If I could combine the DBs, I would, but it is not feasible at this point.

Am I going about this all wrong?
Kristine
 
Try giving Policy.Name and PolicyID aliases in the first query dont forget to change them in all queries which reference them as well.

e.g.

select Policy.Name AS Pname1, PolicyID as Pname2
 
I am beginning to think what I want to do is impossible...

I have made a few modifications, but I wont bore you with the query again. I don't think there is anything structurally wrong with the query. The problem is in what I want to accomplish with it.

For example: In the following Q of Qs how would I reference the #Name# feild? #CombinedQuery.WHAT?#

<cfquery datasource=&quot;DataName1&quot; name=&quot;QueryName1&quot;>
SELECT Table1.Name as Name1, Table1.Email as Email1
FROM Table1
ORDER BY Table1.Name ASC
</cfquery>

<cfquery datasource=&quot;DataName2&quot; name=&quot;QueryName2&quot;>
SELECT Table2.Name as Name2, Table2.Email as Email2
FROM Table2
ORDER BY Table2.Name ASC
</cfquery>


<cfquery dbtype=&quot;query&quot; name=&quot;CombinedQuery&quot;>
SELECT QueryName1.Name1, QueryName1.Email1, QueryName2.Name2,
QueryName2.Email2
FROM QueryName1,QueryName2
</cfquery>

Am I asking for the moon? [sadeyes]

Kristine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top