Skip to main content
Inspiring
September 16, 2019
Question

Left outer join from two datasources

  • September 16, 2019
  • 3 replies
  • 1755 views

This is an age old question, and we're currently using an age old version (CF9), upgrading soon.  I'm a developer of 25 years but have only been using CF for about 8 mos.

 

I have to get data from two different datasources and join them together.  Outer join, of course.  I've read many topics on this.  I had originally done this by looping at a query of the main table and selecting from a query from second table.  My supervisor & former CF programmer doesn't want us to use a loop/select. They first suggested a UNION of the two tables but that's not giving me the results I need.

 

I thought of selecting from one table then inserting them into a temp table on the second datasource but when I try to do an insert into the temp table then selecting from the query of the first table, CF tells me I can't do that either with error "Executing the SQL statement is not allowed."

Any suggestions on correct ways to outer join tables from two datasources without using a loop?

 

Thanks from "The new guy."

-=Rob=-

This topic has been closed for replies.

3 replies

Inspiring
September 19, 2019

Are the two datasources on the same SQL instance?

If so its possible just doing a normal query.

 

SELECT table.field1, table.field2, table.field3, databasename.dbo.tablename.field1 AS otherField1
FROM table
LEFT OUTER JOIN databasename.dbo.tablename ON databasename.dbo.tablename.field1 = table.field
WHERE table.field1 = 1

 

 

RobCatmanAuthor
Inspiring
September 19, 2019
Sadly, no. 😞
EddieLotter
Inspiring
September 24, 2019
In SQL Server I create "linked servers" so that I can join tables from multiple servers. Perhaps your database engine has a similar feature?
Participating Frequently
September 16, 2019

You can query each table from separate datasources then use the two queries in a Query of Queries. Unfortunately you cannot directly do a left outer join in Q of Q but there is an article on how to get the same result at the following https://coldfusionblog.wordpress.com/2012/01/11/how-to-do-an-outer-join-in-query-of-queries/

WolfShade
Legend
September 16, 2019

Hello, Rob,

 

First of all, I'd like to mention that selecting from two different datasources, as you have put in your question, isn't possible, AFAIK.  If you mean from two different TABLES in the same datasource, then yes, that can be done very easily.  And I totally agree with your supervisor.  Iterating through one query to get information from another query is high overhead.  It's more CPU intensive.

 

How you join, though, depends upon what you want.  If you want to get information from both tables regardless of whether or not related records are in both, you do a LEFT OUTER JOIN.  If you don't want records from the left table that don't have a corresponding record(s) in the right table, then a standard LEFT JOIN will skip records in the left table that do not have a corresponding record(s) in the right table.

 

HTH,

 

^ _ ^

RobCatmanAuthor
Inspiring
September 16, 2019

Thanks WolfShade. I've been using SQL based languages for 20 years so I understand how to use joins between two tables. Thank you for your answer, though.

I have to join them from two different datasources.  I can do a QoQ and do a regular join, but it's that darn outer join that's killing me.  Like I said, I've seen this posted many times but I haven't found a actual solution other than using loops.

I was hoping the thouoght of inserting data from a query into a temp table on the second datasource would work, but it's telling me I can't execute that.

 

WolfShade
Legend
September 16, 2019
Yeah, between two actual datasources, you can't do a JOIN or a UNION, AFAIK. Sadly, in this situation, I believe the loop is the only option. It's my understanding that even with full permissions granted, two different datasources cannot interact with each other.. not even to put data into a temp table.