# Multiple SQL statements in a named query

**URL:** <https://forum.inductiveautomation.com/t/multiple-sql-statements-in-a-named-query/32361>\
**Category:** Ignition\
**Created:** [February 18, 2020, 1:26am UTC](https://forum.inductiveautomation.com/t/multiple-sql-statements-in-a-named-query/32361 "2020-02-18T01:26:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbeisner1](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@jbeisner1](https://forum.inductiveautomation.com/u/jbeisner1)\
**Post date:** [February 18, 2020, 1:26am UTC](https://forum.inductiveautomation.com/t/multiple-sql-statements-in-a-named-query/32361/1 "2020-02-18T01:26:46Z")

</div>

Is it possible to execute multiple SQL statements from a single named query?

Something like the following in normal SQL:

BEGIN TRANSACTION

UPDATE tableA…

UPDATE tableB…

COMMIT

---

<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:** [February 18, 2020, 1:54am UTC](https://forum.inductiveautomation.com/t/multiple-sql-statements-in-a-named-query/32361/2 "2020-02-18T01:54:56Z")

</div>

It is not supposed to be supported in JDBC, but some drivers allow it. If supported, it will almost certainly have to be run via scripting, where the Query vs. Update mode is dictated by the function used, instead of deciphered from the query text.

BTW, you say “normal SQL”. Combining multiple statements into a script is not officially part of the SQL specification, and there are incompatibilities between DB brands.

---

<div class="post-metadata">

**Author:** ![hankhaines](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@hankhaines](https://forum.inductiveautomation.com/u/hankhaines)\
**Post date:** [February 10, 2021, 4:14pm UTC](https://forum.inductiveautomation.com/t/multiple-sql-statements-in-a-named-query/32361/3 "2021-02-10T16:14:12Z")

</div>

Ultimately, it would work to allow all of the text included to be executed by a JDBC statement would it not? What parsing is going on to get annoyed by the ‘;’ or other statement separator?

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [February 10, 2021, 4:50pm UTC](https://forum.inductiveautomation.com/t/multiple-sql-statements-in-a-named-query/32361/4 "2021-02-10T16:50:36Z")

</div>

While not what directly what you are asking about, I am happily doing this into a MS SQL connector via a named query:

```auto
IF NOT (SELECT 1 FROM table WHERE id = :id_param)
BEGIN
  INSERT INTO table (id, field) VALUES(:id_param, :field_param)
END

```

So complex statements are supported to some degree. My suggestion is to try what you want with your DB of choice and see what happens.

---

<div class="post-metadata">

**Author:** ![justin.brzozoski](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justin.brzozoski/32/13452_2.png) [@justin.brzozoski](https://forum.inductiveautomation.com/u/justin.brzozoski)\
**Post date:** [April 12, 2021, 6:00pm UTC](https://forum.inductiveautomation.com/t/multiple-sql-statements-in-a-named-query/32361/5 "2021-04-12T18:00:19Z")

</div>

In case someone else stumbles on this because they looked in the wrong place first, IA provides scripting functions like [`system.db.beginNamedQueryTransaction`](https://docs.inductiveautomation.com/display/DOC81/system.db.beginNamedQueryTransaction) and [`rollbackTransaction`](https://docs.inductiveautomation.com/display/DOC81/system.db.rollbackTransaction) that let you wrap multiple named queries into a single transaction block.

---

<div class="post-metadata">

**Author:** ![eric.stricklin](https://avatars.discourse-cdn.com/v4/letter/e/ad7895/32.png) [@eric.stricklin](https://forum.inductiveautomation.com/u/eric.stricklin)\
**Post date:** [April 12, 2021, 6:48pm UTC](https://forum.inductiveautomation.com/t/multiple-sql-statements-in-a-named-query/32361/6 "2021-04-12T18:48:24Z")

</div>

I recommend if you have a complex SQL query with multiple statements, store it in a SPROC, and just call the SPROC from your named query.

```auto
EXEC mySPROC :myparameter

```
