# Calling named query from scripting

**URL:** <https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294>\
**Category:** Ignition\
**Created:** [June 28, 2022, 1:28pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294 "2022-06-28T13:28:37Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![PRAMANJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pramanj/32/66935_2.png) [@PRAMANJ](https://forum.inductiveautomation.com/u/PRAMANJ)\
**Post date:** [June 28, 2022, 1:28pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/1 "2022-06-28T13:28:37Z")

</div>

I get error

**java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Project name required for script execution: the Gateway Scripting Project is not set.**

while calling **`system.db.runNamedQuery(` `"myUpdateQuery"` `, params)`** from a script.

From the [documentation](https://docs.inductiveautomation.com/display/DOC80/system.db.runNamedQuery) it appears we have to give "full path" of query name for a project scoped query or project name for a gateway scoped query

**`system.db.runNamedQuery(` `"ProjectName"` `, ` `"folderName/myNamedQuery"` `, {})`**

What is full path of query? When I right click and "copy path" from the named query query I just get name of the query? Are there any restrictions on query names like no blanks , case sensitivity etc. How does it distinguish between the two scopes!

---

<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:** [June 28, 2022, 1:49pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/2 "2022-06-28T13:49:56Z")

</div>

> [@prafull\_j](#):
>
> How does it distinguish between the two scopes!

By where you call from.

Tag events (defined on the tag) are gateway scope, outside of any project. The gateway scripting project (which you apparently have not configured) would be a default for some cases.

---

<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:** [June 28, 2022, 1:58pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/3 "2022-06-28T13:58:05Z")

</div>

Here is one way I learned on here on how to tell if you’re in a Vision scope

```auto
def isVision():
	"""
	Tells us if we are in the vision client or not. Only works in client scope.
	"""
	import system.tag
	return system.tag.exists('[System]Client/User/Username')

```

You can use this to tell if you are in vision and and use the `system.db..runNamedquery("myUpdatequery", params)` version, and if not use the `system.db.runNamedQuery("Project Name", "myUpdateQuery", params)` version.

It is annoying to do it this way but unless a change is made to system.db.runNamedQuery’s function calls/param setup, there’s no way around it.

---

<div class="post-metadata">

**Author:** ![PRAMANJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pramanj/32/66935_2.png) [@PRAMANJ](https://forum.inductiveautomation.com/u/PRAMANJ)\
**Post date:** [June 28, 2022, 2:28pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/4 "2022-06-28T14:28:23Z")

</div>

Ok got it. My script is from the alarm notification pipeline to call calculatedRoster function so obviously its in gateway scope! So I will pass the project name parameter and see if it works!

---

<div class="post-metadata">

**Author:** ![PRAMANJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pramanj/32/66935_2.png) [@PRAMANJ](https://forum.inductiveautomation.com/u/PRAMANJ)\
**Post date:** [June 28, 2022, 2:33pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/5 "2022-06-28T14:33:17Z")

</div>

> [@bkarabinchak.psi](#):
>
> It is annoying to do it this way but unless a change is made to system.db.runNamedQuery’s function calls/param setup, there’s no way around it.

Nice way to check the scope. However we ought to know the scope ourselves based on where its called from. For me I thought its client scope but I realize now that its in gateway scope.

---

<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:** [June 28, 2022, 2:35pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/6 "2022-06-28T14:35:43Z")

</div>

Agreed you should know what scope you are working in. Sometimes you want a script to be available in multiple scopes. I had a script that used `system.db.runNamedQuery` that was callable in both a vision scope and a gateway scope. So I needed to do

```auto
if env.isVision():
    system.db.runNamedQuery("someUpdateQuery", params)
else:
    system.db.runNamedQuery("Some Project", "someUpdateQuery", params)

```

so that it would work in both situations.

---

<div class="post-metadata">

**Author:** ![PRAMANJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pramanj/32/66935_2.png) [@PRAMANJ](https://forum.inductiveautomation.com/u/PRAMANJ)\
**Post date:** [June 28, 2022, 2:40pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/7 "2022-06-28T14:40:32Z")

</div>

Yes agreed. I didn't think of this use case. Buts it's an elegant solution! One can comeup with such solutions only with deep understanding of scripting in Ignition. Thanks it might be useful in some situations.

---

<div class="post-metadata">

**Author:** ![PRAMANJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pramanj/32/66935_2.png) [@PRAMANJ](https://forum.inductiveautomation.com/u/PRAMANJ)\
**Post date:** [June 28, 2022, 2:41pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/8 "2022-06-28T14:41:21Z")

</div>

Yesss! It worked like a champ! Thanks a lot!

---

<div class="post-metadata">

**Author:** ![PRAMANJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pramanj/32/66935_2.png) [@PRAMANJ](https://forum.inductiveautomation.com/u/PRAMANJ)\
**Post date:** [June 28, 2022, 2:45pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/9 "2022-06-28T14:45:43Z")

</div>

I hardcoded the project name for quick testing. However how to get project name dynamically!? I guess some system tag will give it but would be nice to be spoon fed!

---

<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:** [June 28, 2022, 2:52pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/10 "2022-06-28T14:52:30Z")

</div>

Not sure exactly what context you are working in but this may help

[https://docs.inductiveautomation.com/display/DOC80/system.util.getProjectName](https://docs.inductiveautomation.com/display/DOC80/system.util.getProjectName)

---

<div class="post-metadata">

**Author:** ![PRAMANJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pramanj/32/66935_2.png) [@PRAMANJ](https://forum.inductiveautomation.com/u/PRAMANJ)\
**Post date:** [June 28, 2022, 4:26pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/11 "2022-06-28T16:26:11Z")

</div>

Thanks a lot, I will change it to this function right away.

---

<div class="post-metadata">

**Author:** ![jacque\_trahan](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jacque_trahan/32/58886_2.png) [@jacque\_trahan](https://forum.inductiveautomation.com/u/jacque_trahan)\
**Post date:** [January 22, 2024, 3:28pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/12 "2024-01-22T15:28:31Z")

</div>

I have a follow up questions on this.

I made a named SQL query to use in my alarm pipeline. When I do work in the alarm pipeline I use the scripting Console to test code section by section then add it to the pipeline until it is completed.

My question is dealing with the following two aspect. When I was calling my named query from the Scription console. I could use:

> `system.db.runNamedQuery("folderName/myNamedQuery")

But when I went into the pipeline I had to use:

> system.db.runNamedQuery("ProjectName", "folderName/myNamedQuery")

Why does the scope change like that?

And if this is a normal aspect that can happen, will there be any update to the script console, say a tab for each scope to practice in? That would be really nice to have.

---

<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:** [January 22, 2024, 3:51pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/13 "2024-01-22T15:51:21Z")

</div>

Pipelines run in the gateway. The designer is a separate process (not just separate, but _different_), even if run on the gateway machine. It simply **cannot** directly run something as gateway scope.

There are feature requests floating around to make some test environment that would hand a script from the designer to test run in the gateway, but that is a remarkably malware-ish behavior.

Consider making a gateway message handler that delegates to a project library script function you wish to test in gateway scope. Then it can be invoked from the designer scripting console with `system.util.sendRequest`.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [January 22, 2024, 3:52pm UTC](https://forum.inductiveautomation.com/t/calling-named-query-from-scripting/61294/14 "2024-01-22T15:52:29Z")

</div>

Tip: format code as **code** using the \</\> button, not quotations using the ” button (or `>` prefix). For more see [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).
