# SQL From Multiple Sources

**URL:** <https://forum.inductiveautomation.com/t/sql-from-multiple-sources/67188>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [December 2, 2022, 5:10pm UTC](https://forum.inductiveautomation.com/t/sql-from-multiple-sources/67188 "2022-12-02T17:10:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnBomb](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/johnbomb/32/38431_2.png) [@JohnBomb](https://forum.inductiveautomation.com/u/JohnBomb)\
**Post date:** [December 2, 2022, 5:10pm UTC](https://forum.inductiveautomation.com/t/sql-from-multiple-sources/67188/1 "2022-12-02T17:10:30Z")

</div>

Is there a way to do SQL functions like joins between tables in two different database connections in Ignition? I have two different database servers (One MSSQL and One MySQL) and I'm trying to join data between tables in both databases. Is this possible? It seems like queries are limited to a single database connection and I can't select tables from multiple connections for one query. Any ideas, besides something very inefficient like reading everything into dataset variables in a script or something and then iterating through the entire dataset to select the data I want?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 2, 2022, 5:14pm UTC](https://forum.inductiveautomation.com/t/sql-from-multiple-sources/67188/2 "2022-12-02T17:14:27Z")

</div>

Not possible. (Not an Ignition limitation, but the fundamentals of JDBC connections and database connections in general.) You must make separate queries and script the join operations. Many databases support DB-to-DB "foreign data" connections, which make multiple DBs appear as one, and permit this kind of operation. Generally not between different brands, though.

---

<div class="post-metadata">

**Author:** ![ShaneH](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@ShaneH](https://forum.inductiveautomation.com/u/ShaneH)\
**Post date:** [December 2, 2022, 5:16pm UTC](https://forum.inductiveautomation.com/t/sql-from-multiple-sources/67188/3 "2022-12-02T17:16:52Z")

</div>

Since they are located on different servers, I dont think so.

FWIW... (at least in SQL Server, using the SQL server driver, etc.) ...

If the two databases are available on the same server... then you could try using a fully qualified name for each table from your Ignition query.

```auto
SELECT *
FROM db1.dbo.tbl1 AS T1
LEFT JOIN db2.dbo.tbl2 AS T2
ON T2.id = T1.id

```

Your best bet might be to setup a replicated table or view in one of the databases to the table in the second database. That would be a job for your DBA though.

HTH.  
-Shane

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [December 2, 2022, 5:17pm UTC](https://forum.inductiveautomation.com/t/sql-from-multiple-sources/67188/4 "2022-12-02T17:17:48Z")

</div>

> [@pturmel](#):
>
> Generally not between different brands, though.

Obviously the solution is to stand up a _third_ DB using our lord and saviour Postgres, which has FDWs available for both MSSQL and MySQL, supposedly:  
[https://wiki.postgresql.org/wiki/Foreign\_data\_wrappers#Specific\_SQL\_Database\_Wrappers](https://wiki.postgresql.org/wiki/Foreign_data_wrappers#Specific_SQL_Database_Wrappers)

Disclaimer: Don't actually do this. Probably.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 2, 2022, 5:20pm UTC](https://forum.inductiveautomation.com/t/sql-from-multiple-sources/67188/5 "2022-12-02T17:20:07Z")

</div>

> [@paul-griffith](#):
>
> which has FDWs available for both MSSQL and MySQL

Huh. Did not know my favorite DB had those. I **would** do that. No "probably" about it. That would almost certainly smoke any jython join.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 2, 2022, 7:59pm UTC](https://forum.inductiveautomation.com/t/sql-from-multiple-sources/67188/6 "2022-12-02T19:59:15Z")

</div>

> [@paul-griffith](#):
>
> Disclaimer: Don't actually do this. Probably.

> [@pturmel](#):
>
> I **would** do that.

CHALLENGE ACCEPTED! Or hold my beer. Not sure which one applies here.

---

<div class="post-metadata">

**Author:** ![eric.hollering](https://avatars.discourse-cdn.com/v4/letter/e/ad7895/32.png) [@eric.hollering](https://forum.inductiveautomation.com/u/eric.hollering)\
**Post date:** [December 2, 2022, 9:14pm UTC](https://forum.inductiveautomation.com/t/sql-from-multiple-sources/67188/8 "2022-12-02T21:14:32Z")

</div>

Another option might be something like Denodo that does some of the backend abstraction for you. They do have a JDBC driver it appears.

Full disclosure: I have not worked with Denodo myself, and have no idea how well it works or what it costs.

---

<div class="post-metadata">

**Author:** ![jkramer](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@jkramer](https://forum.inductiveautomation.com/u/jkramer)\
**Post date:** [January 3, 2023, 9:00pm UTC](https://forum.inductiveautomation.com/t/sql-from-multiple-sources/67188/9 "2023-01-03T21:00:53Z")

</div>

How about creating a linked server on your MSSQL server that connects to the MySQL database, and then doing the join in MSSQL. I'm pretty sure I've done something similar to that.
