# runNamedQuery vs runPrepQuery

**URL:** <https://forum.inductiveautomation.com/t/runnamedquery-vs-runprepquery/81268>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [October 31, 2023, 7:34pm UTC](https://forum.inductiveautomation.com/t/runnamedquery-vs-runprepquery/81268 "2023-10-31T19:34:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyler\_Shoemake](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tyler_shoemake/32/55661_2.png) [@Tyler\_Shoemake](https://forum.inductiveautomation.com/u/Tyler_Shoemake)\
**Post date:** [October 31, 2023, 7:34pm UTC](https://forum.inductiveautomation.com/t/runnamedquery-vs-runprepquery/81268/1 "2023-10-31T19:34:23Z")

</div>

I am curious about the best practices when using system.db.runNamedQuery. What are the advantages and disadvantages to using system.db.runNamedQuery instead of using any of the other system functions like system.db.runPrepQuery, runPrepUpdate, runQuery, etc.

---

<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:** [October 31, 2023, 7:37pm UTC](https://forum.inductiveautomation.com/t/runnamedquery-vs-runprepquery/81268/2 "2023-10-31T19:37:17Z")

</div>

There's a couple of advantages:

- Convenience; they are defined in a single place and can be called/referenced from multiple locations.
- Security; clients cannot modify the query in any way, they are simply passing parameters to a pre-defined query instead.

The user manual talks about them in more detail: [Named Queries - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/Named+Queries)

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [October 31, 2023, 7:47pm UTC](https://forum.inductiveautomation.com/t/runnamedquery-vs-runprepquery/81268/3 "2023-10-31T19:47:30Z")

</div>

I'll add that the same named queries can be utilized from Perspective and Vision as well. Then it is only a single source to database work.

---

<div class="post-metadata">

**Author:** ![Tyler\_Shoemake](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tyler_shoemake/32/55661_2.png) [@Tyler\_Shoemake](https://forum.inductiveautomation.com/u/Tyler_Shoemake)\
**Post date:** [October 31, 2023, 7:56pm UTC](https://forum.inductiveautomation.com/t/runnamedquery-vs-runprepquery/81268/4 "2023-10-31T19:56:11Z")

</div>

I am curious as to any situations it might be better to not use a named query? Anyone know of any?

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [October 31, 2023, 8:01pm UTC](https://forum.inductiveautomation.com/t/runnamedquery-vs-runprepquery/81268/5 "2023-10-31T20:01:10Z")

</div>

If/when you have a dynamic list of columns and data to access. Sometimes you just don't know which columns you will need to access and construction that at runtime is necessary.

That being said, most of the time named queries fit the bill.

---

<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:** [October 31, 2023, 8:29pm UTC](https://forum.inductiveautomation.com/t/runnamedquery-vs-runprepquery/81268/6 "2023-10-31T20:29:57Z")

</div>

The original purpose of Named Queries was to make it possible to pass values into Vision Query bindings in a secure and efficient way, solving the unreliability of parameter stringification (looking at you, `java.util.Date`), and allowing the huge "legacy DB access" security hole to be closed.

The maintenance advantages of NQs can be matched by multi-line strings for SQL in library scripts, so I tend to discount that item.

However, not all is well. Some things to consider:

- NQs permit the use of `QueryString` parameters, which is the insecure stringification behavior brought back from the dead. And it passes through without the Legacy DB security control, so you aren't necessarily as safe with NQs as one might think.

- NQs perform value parameter substitution with colon-delimited placeholders instead of the JDBC-compliant question mark placeholders. Which means Ignition itself is responsible for that operation, not any JDBC driver. Where JDBC drivers support SQL **scripts** , they work **completely correctly** , with comments and everything, via any of the `system.db.*Prep*()` functions. NQs often break with SQL **scripts**.

- As noted, NQs can only handle variable numbers of parameters via the insecure QueryString mechanism. Prep queries can be constructed with variable numbers of question-mark placeholders, and matching variable-length lists of securely-handled parameters.

You should be comfortable with **both** approaches, so you can apply the right tool for any particular task.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 1, 2023, 10:53am UTC](https://forum.inductiveautomation.com/t/runnamedquery-vs-runprepquery/81268/7 "2023-11-01T10:53:37Z")

</div>

~~One use case I had that was better for me to use prepQuery vs a namedQuery was when I had gateway that was attached to multiple databases with the same schemas and I needed to query from different ones.~~

 

With Named Queries, I would have had to make the same query 4 times, each with a different database connection set in the Authoring tab of the named query, but the SQL being the same.

~~With Prepared Queries, I wrote the query once in a library script function and modified =the database connection string as needed.~~

Having said that, I do aim to use named queries first and foremost now because it is more secure for vision and while it's true you can replicate the single source of truth with library functions, I like that the Named Query editor does do SQL keyword highlighting - it makes reading it the SQL itself a bit easier than when it's all pink inside a triple quote - but this is purely preferential.

As Phil said, right tool for the right job.

---

<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:** [November 1, 2023, 11:08am UTC](https://forum.inductiveautomation.com/t/runnamedquery-vs-runprepquery/81268/8 "2023-11-01T11:08:06Z")

</div>

> [@bkarabinchak.psi](#):
>
> With Named Queries, I would have had to make the same query 4 times, each with a different database connection set in the Authoring tab of the named query, but the SQL being the same.

Pssst! NQs can parameterize the DB connection....

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [November 1, 2023, 11:13am UTC](https://forum.inductiveautomation.com/t/runnamedquery-vs-runprepquery/81268/9 "2023-11-01T11:13:22Z")

</div>

~~How would you do it ? It doesn't mention it anywhere in the docs for [system.db.runNamedQuery - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/system.db.runNamedQuery) about changing the db connection when you call it. I had tried to with `db=`, `database=`, or putting the connection name at the end, but it kept using what I set in authoring so I gave up and used to prepared queries.~~

 

~~I believe you, I just wish I knew this before and it was in the docs.~~

I see it now. You can set the database connection to Parameter. Somehow I missed that. Thanks.
