Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.
-
wilhelm #1
Query within a Query
:confused;
Is it possible to do a query within a query to where the data wanted is in 2
seperate Datasources? The reason is that we are being asked to combine info
from 2 seperate databases and would not like to use looping if possible.
code example
<cfquery name="quepeople" datasource="database1">
Select name, address, shift, supervisor, idnumber
From employeeinfo
where
<cfquery name="queawardrecipeant" datasource="database2">
Select idnumber
from Awardtable
where presented <> 0
<cfquery>
<cfquery>
wilhelm Guest
-
Query of Queries on query New type query
In CF5 we have a page that creates a query, using queryNew and querySetCell and the like, we then used dbtype="query" and gave it's name so we could... -
query of query throwing weird exception
One workaround: I had added rows to a cfsearch query, and set a numeric value in custom1 field that that query provides. A query of queries... -
Convert a query to a list, or find an item in a query
Hi All, I am using CFPOP to retrieve mail from a server, then delete each message after I retrieve it. What I want to do is to check that I don;t... -
CAML Query: Multiple Query Fields Issue
I need to Create a CAML Query Dynamically with VB to a Sharepoint WebService GetListItems Method. The User Could Select 1 to X Number of IDs... -
BCP query out executed by xp_cmdshell works fine from query analyzer but fails from VB Component
Hi all, I have a stored procedure which returns a vast number of record and i have to write the output into a csv file. I'm using BCP utility to... -
-
mxstu #3
Re: Query within a Query
Depending on what type of databases you're talking about, it may be possible to
run a query on tables that reside in two different databases. In SQL Server,
you can query tables in two different databases or on two different linked
servers, just as if they were objects in the same database. You just need to
qualify the object names with the server and database names of each objects:
--- psuedo code
SELECT db1.ColumnID AS LocalColumn, db2.ColumnID AS RemoteColumn
FROM localServerName.databaseName.owner.tableName db1,
remoteServerName.databaseName.owner.talbeName db2
WHERE (some conditions....)
mxstu Guest
-
MikerRoo #4
Re: Query within a Query
You can easily accomplish something similar using query of queries -- although
this may not be as efficient as querying two DB's simultaneously.
See the code below:
Regards,
-- MikeR
<cfquery name="quepeople" datasource="database1">
SELECT name, address, shift, supervisor, idnumber
FROM employeeinfo
</cfquery>
<cfquery name="queawardrecipeant" datasource="database2">
SELECT idnumber
FROM Awardtable
WHERE presented <> 0
</cfquery>
<cfquery name="qGetAwardVictims" dbtype="Query">
SELECT DISTINCT
quepeople.*
FROM
quepeople,
queawardrecipeant
WHERE
quepeople.idnumber IN (queawardrecipeant.idnumber)
</cfquery>
MikerRoo Guest
-
OldCFer #5
Re: Query within a Query
Another way would be to create a view in your DB linking the two tables.
OldCFer Guest
-
Alen #6
Re: Query within a Query
you may be able to use ValueList(query.column) to make a list and then use that list like this:
WHERE idnumber IN ( #ValueList(query.column)# )
Alen Guest
-
wilhelm #7
Re: Query within a Query
How would you go about doing this. Would you create a list (or array) for the first then use the list as part of the wuery for the 2nd one or how.
wilhelm Guest
-
mxstu #8
Re: Query within a Query
I believe allan was suggesting that you could run the first query, and then use
the ValueList() function to create a list of ID's that you could then use in
your second query.
<!--- note - this does not handle an empty list --->
<cfquery name="getFirstTableData" datasource="yourFirstDSN">
SELECT ID, SomeOtherColumns
FROM FirstDataSourceTable
</cfquery>
<cfset IDList = ValueList(getFirstTableData.ID)>
<cfquery name="getSecondTableData" datasource="yourSecondDSN">
SELECT ID, SomeOtherColumns
FROM SecondDataSourceTable
WHERE ID IN (#IDList#)
</cfquery>
mxstu Guest
-
TA-Selene #9
Re: Query within a Query
Depending on the number of values that could returned, using a query of a query
as MikerRoo suggeted may be your best bet. There is a limit on the lenght of a
list you can pass into a query and speed can be a major factor. I once had a
developer pass in a list like this and it had thousands of values. Needless to
say I rewrote the query to be more efficient.
TA-Selene Guest



Reply With Quote

