# Named Queries vs. queries in script library - Best Practices

**URL:** <https://forum.inductiveautomation.com/t/named-queries-vs-queries-in-script-library-best-practices/105841>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [May 2, 2025, 7:48pm UTC](https://forum.inductiveautomation.com/t/named-queries-vs-queries-in-script-library-best-practices/105841 "2025-05-02T19:48:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hw79](https://avatars.discourse-cdn.com/v4/letter/h/838e76/32.png) [@hw79](https://forum.inductiveautomation.com/u/hw79)\
**Post date:** [May 2, 2025, 7:48pm UTC](https://forum.inductiveautomation.com/t/named-queries-vs-queries-in-script-library-best-practices/105841/1 "2025-05-02T19:48:50Z")

</div>

Somewhere in the forums I remember reading that it was a better practice to write queries in a script library instead of using named queries. I can't find the topic that it was mentioned on. So, what are everyone's thoughts on this? Arguments for and against using named queries?

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [May 2, 2025, 8:06pm UTC](https://forum.inductiveautomation.com/t/named-queries-vs-queries-in-script-library-best-practices/105841/2 "2025-05-02T20:06:54Z")

</div>

I use named queries all the time in place of scripted queries. It basically allows for resistance against SQL injection when using them properly. You can do prepared statements/queries in scripting also, but I like having a nice interface to test queries out and just call them from the script with whatever parameters I need.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [May 2, 2025, 11:34pm UTC](https://forum.inductiveautomation.com/t/named-queries-vs-queries-in-script-library-best-practices/105841/3 "2025-05-02T23:34:50Z")

</div>

> [@michael.flagler](#):
>
> resistance against SQL injection when using them properly

So do the 'prep' scripting functions.

You can't create dynamic queries, perhaps obviously, using named queries. Named queries should be used for all static queries and dynamic ones must use scripted ones making sure to validate dynamic parts against an allow list

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 3, 2025, 1:07am UTC](https://forum.inductiveautomation.com/t/named-queries-vs-queries-in-script-library-best-practices/105841/4 "2025-05-03T01:07:01Z")

</div>

> [@hw79](#):
>
> I remember reading that it was a better practice to write queries in a script library instead of using named queries

Generally, named queries are considered to be the better practice however, it is situational.

> [@runprepQuery or runNamedQuery - Best Practice](https://forum.inductiveautomation.com/t/runprepquery-or-runnamedquery-best-practice/85792/2):
>
> With the exception of some special cases (ahmmm QueryString parameters). You should almost always default to Named Queries. Performance wise, I doubt there is anything more than negligible gains or losses between the two functions in a scripting environment. They both use the same JDBC driver, so the same queries will basically run exactly the same. Although, I will admit that I don't know if Named Query caching applies to the scripting function or just bindings, so that could make a differe…

---

<div class="post-metadata">

**Author:** ![louagejasper](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/louagejasper/32/86311_2.png) [@louagejasper](https://forum.inductiveautomation.com/u/louagejasper)\
**Post date:** [May 5, 2025, 9:27am UTC](https://forum.inductiveautomation.com/t/named-queries-vs-queries-in-script-library-best-practices/105841/5 "2025-05-05T09:27:39Z")

</div>

I always used named queries just until recently. I now use queries in scripts, mainly because my workflow changed so much in the past 2 years.

In our experience, when creating a new table in your database, you always want the same named queries and the same scripts (insert, update, upsert, delete, getById, getByfilter, ...).

So I made a AI-helper, where I just ask: 'Create me a table production\_orders with fields 'no', 'line\_id' -\> FK to lines, 'capacity', ... and give me the jython functions.

This returns the sql command  
'CREATE TABLE production\_orders .... ' with the fields I want (plus extra fields like id, created\_by, created\_at, ...

And then it returns the jython code with all the prep queries (insert, update, upsert, delete, getById, getByfilter, ...).  
I could have the AI return all the named queries, but just having 1 script makes the workflow 5 times faster, because it's just copy-paste.

We also extended this so it has function converting the query result to classes immediately, so class logic can be used.

---

<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:** [May 5, 2025, 12:18pm UTC](https://forum.inductiveautomation.com/t/named-queries-vs-queries-in-script-library-best-practices/105841/6 "2025-05-05T12:18:08Z")

</div>

I can't find it at the moment but I recall making a post where I tested named vs prep query vs plain old runUpdateQuery with %s style, and named came out on top, prep second and plain was last. If performance matters named is definitely the best option.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 5, 2025, 12:23pm UTC](https://forum.inductiveautomation.com/t/named-queries-vs-queries-in-script-library-best-practices/105841/7 "2025-05-05T12:23:39Z")

</div>

> [@louagejasper](#):
>
> I could have the AI return all the named queries, but just having 1 script makes the workflow 5 times faster, because it's just copy-paste.

Except for prepQueries (afaik) do not take advantage of caching, which will give Named Queries an edge over this approach. Unless the parameters are always changing, in which case I would expect there to be no real difference. In situations where the parameters are static or don't change often, I would lean on Named Queries, despite the hit to development speed.

---

<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:** [May 5, 2025, 12:28pm UTC](https://forum.inductiveautomation.com/t/named-queries-vs-queries-in-script-library-best-practices/105841/8 "2025-05-05T12:28:06Z")

</div>

And in the case where there might be multiple developers in the code with different levels of familiarity, I find that named queries are easier to read and find in the code.

---

<div class="post-metadata">

**Author:** ![louagejasper](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/louagejasper/32/86311_2.png) [@louagejasper](https://forum.inductiveautomation.com/u/louagejasper)\
**Post date:** [May 5, 2025, 1:20pm UTC](https://forum.inductiveautomation.com/t/named-queries-vs-queries-in-script-library-best-practices/105841/9 "2025-05-05T13:20:28Z")

</div>

@lrose @bschroeder Both valid points!

---

<div class="post-metadata">

**Author:** ![Ryan\_Deardorff](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan_deardorff/32/63815_2.png) [@Ryan\_Deardorff](https://forum.inductiveautomation.com/u/Ryan_Deardorff)\
**Post date:** [May 5, 2025, 3:23pm UTC](https://forum.inductiveautomation.com/t/named-queries-vs-queries-in-script-library-best-practices/105841/10 "2025-05-05T15:23:24Z")

</div>

My only gripe with Named Queries is one that it seems like they could easily address:

> **[Optional parameters with default values for Named Queries | Ignition Features...](https://ideas.inductiveautomation.com/ignition-features-and-ideas/p/optional-parameters-with-default-values-for-named-queries)**
>
> One feature I really love about Python is the ability to add new parameters to functions and give them default values so that I don't have to track down every
