# How do I use 2 databases in one named query

**URL:** <https://forum.inductiveautomation.com/t/how-do-i-use-2-databases-in-one-named-query/89083>\
**Category:** Ignition\
**Created:** [April 18, 2024, 9:30pm UTC](https://forum.inductiveautomation.com/t/how-do-i-use-2-databases-in-one-named-query/89083 "2024-04-18T21:30:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![compuzak](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@compuzak](https://forum.inductiveautomation.com/u/compuzak)\
**Post date:** [April 18, 2024, 9:30pm UTC](https://forum.inductiveautomation.com/t/how-do-i-use-2-databases-in-one-named-query/89083/1 "2024-04-18T21:30:08Z")

</div>

I need to read data from a table in one database and write to a table in another database in a named update query. Both database connections are setup in the same Ignition gateway. I can't figure out the syntax to reference the 2nd database. I tried using both the actual SQL database name and the Ignition SQL database connection name and neither worked. The 1st database is selected in the "database connection" drop down in the update query.

Here is an example of what I need:

UPDATE database2.dbo.TableToUpdate  
SET Field1 = d1.Field1  
FROM  
(SELECT Field1 FROM database1.dbo.TableToRead) as d1

It gives me invalid object on database2.dbo.TableToUpdate. If I try to put a server name in front of database2 it tells me the server name is not in sys.servers. I am not sure if it wants the Ignition server or the SQL server, so I tried both and neither worked. It tells me I can linked the server, but the server is not a linked server, it is the main server.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 18, 2024, 9:31pm UTC](https://forum.inductiveautomation.com/t/how-do-i-use-2-databases-in-one-named-query/89083/2 "2024-04-18T21:31:32Z")

</div>

It's not possible to reference multiple DB connections in a query.

What you're trying to do might be possible if one or both servers are SQL Server and you're using the "linked servers" feature. Otherwise forget it.

---

<div class="post-metadata">

**Author:** ![compuzak](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@compuzak](https://forum.inductiveautomation.com/u/compuzak)\
**Post date:** [April 18, 2024, 9:34pm UTC](https://forum.inductiveautomation.com/t/how-do-i-use-2-databases-in-one-named-query/89083/3 "2024-04-18T21:34:16Z")

</div>

Wow, thanks for the rapid response. Can I make a named select query to return the dataset and reference this query in another update query?

If not, does this have to be done via python scripting?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 18, 2024, 9:35pm UTC](https://forum.inductiveautomation.com/t/how-do-i-use-2-databases-in-one-named-query/89083/4 "2024-04-18T21:35:05Z")

</div>

You'll have to cobble this together in scripting.

---

<div class="post-metadata">

**Author:** ![compuzak](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@compuzak](https://forum.inductiveautomation.com/u/compuzak)\
**Post date:** [April 18, 2024, 9:36pm UTC](https://forum.inductiveautomation.com/t/how-do-i-use-2-databases-in-one-named-query/89083/5 "2024-04-18T21:36:07Z")

</div>

Thank you. Much cleaner in a named query, but scripting will work, thanks,

---

<div class="post-metadata">

**Author:** ![Eike\_H](https://avatars.discourse-cdn.com/v4/letter/e/b38774/32.png) [@Eike\_H](https://forum.inductiveautomation.com/u/Eike_H)\
**Post date:** [April 11, 2025, 12:07pm UTC](https://forum.inductiveautomation.com/t/how-do-i-use-2-databases-in-one-named-query/89083/6 "2025-04-11T12:07:21Z")

</div>

Hey,

I am having a similar problem where I want to transfer data on a sheduled event from one database to another.  
Can I use Tags to save the value and run two separated queries?

---

<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:** [April 11, 2025, 12:10pm UTC](https://forum.inductiveautomation.com/t/how-do-i-use-2-databases-in-one-named-query/89083/7 "2025-04-11T12:10:53Z")

</div>

Pretty sure this still applies, and would be more stable than trying to use tags.

> [@Kevin.Herron](#):
>
> You'll have to cobble this together in scripting.
