# runNamedQuery comprehension syntax options

**URL:** <https://forum.inductiveautomation.com/t/runnamedquery-comprehension-syntax-options/73847>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [May 17, 2023, 4:30pm UTC](https://forum.inductiveautomation.com/t/runnamedquery-comprehension-syntax-options/73847 "2023-05-17T16:30:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cameronin](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Cameronin](https://forum.inductiveautomation.com/u/Cameronin)\
**Post date:** [May 17, 2023, 4:30pm UTC](https://forum.inductiveautomation.com/t/runnamedquery-comprehension-syntax-options/73847/1 "2023-05-17T16:30:48Z")

</div>

I'm trying to tidy up a lot of code I wrote ages ago when I was even worse at it than I am now.  
One thing I want to do just now is to make an array or list of pyDatasets from the same named query run for several machines.

The parameters for the names query are id, start, and end.

I've had a look through the forum and this post seemed helpful:

> [@Proper system.db.runNamedQuery usage in python](https://forum.inductiveautomation.com/t/proper-system-db-runnamedquery-usage-in-python/29768/2):
>
> # Not a great example, but (something very much like) this works for me roles = system.dataset.toPyDataSet(system.db.runNamedQuery(“UserManagement/Get Roles for a UserName”, {‘UserName’:row.get(‘username’)})) entries = [] for i in range (len(roles)): entries.append(roles[i][0]) return ', '.join(map(str, entries)) You should note that in my example I’m using 8.0.6, so runNamedQuery no longer requires a project name argument. I have no idea how you’re acquiring the row variable you’re using t…

so I've started there.

The code I have at the moment is:

```auto
def transform(self, value, quality, timestamp):
	
	machineDict = self.getChild("root").getChild("oeeRunTime").getChild("flxSettings").getChild("flxSetUp").getChild("machineID").props.value
	
	machinePy = system.dataset.toPyDataSet(
		system.db.runNamedQuery(
		"Script/function for id",
		{'id':[machineDict[v]['machID'] for v in range(len(machineDict))],
		'start':self.custom.startTime,
		'end':system.date.addHours(self.custom.startTime,8)}
		)
	)

	return machinePy

```

I'm getting an Error\_ScriptEval. It's a long message, and I don't know how to copy it (I can just see it as hover text - it says "(Unknown Source)" a lot of times...

Any help on whether this is a thing it is possible to do, or not, and in which ways I have done or approached it wrong greatly appreciated 😛

Thanks  
Cameron

---

<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 17, 2023, 4:40pm UTC](https://forum.inductiveautomation.com/t/runnamedquery-comprehension-syntax-options/73847/2 "2023-05-17T16:40:01Z")

</div>

> [@Cameronin](#):
>
> ```auto
> {'id':[machineDict[v]['machID'] for v in range(len(machineDict))],
> 
> ```

I don't think this will ever work. I don't think any of the named query parameter types (or even query string) know what to do with a python list. Even if it's just passed through as a string, that's probably causing incorrect SQL syntax.

I think what you really need to do is something like

```auto
machinePy = None
endTime = system.date.addHours(self.custom.startTime,8)
startTime = self.custom.startTime
for v in range(len(machineDict)):
    if machinePy is None:
        machinePy = system.db.runNamedQuery("yourNamedQuery", {"id":machineDict[v]['machID'], 'start':startTime, 'end':endTime})
    else:
        newDs = system.db.runNamedQuery("yourNamedQuery", {"id":machineDict[v]['machID'], 'start':startTime, 'end':endTime})
        machinePy = system.dataset.appendDataset(machinePy, newDs)

return machinePy

```

This is just rearranging your way.

There are better ways to go through a dictionary. You can do something like

```auto
for key, value in machineDict.items():

```

And this can simplify things further. I leave this as an exercise for the reader 🙂

I also think you need a project parameter for named queries in perspective?

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [May 17, 2023, 4:42pm UTC](https://forum.inductiveautomation.com/t/runnamedquery-comprehension-syntax-options/73847/3 "2023-05-17T16:42:12Z")

</div>

Also, within Perspective scripts always qualify as [gateway-scoped](https://docs.inductiveautomation.com/display/DOC81/system.db.runNamedQuery#:~:text=Gateway%20Scope%20Syntax), so you'll need to include the Project name as an argument of the runNamedQuery invocation.

---

<div class="post-metadata">

**Author:** ![Cameronin](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Cameronin](https://forum.inductiveautomation.com/u/Cameronin)\
**Post date:** [May 18, 2023, 9:03am UTC](https://forum.inductiveautomation.com/t/runnamedquery-comprehension-syntax-options/73847/4 "2023-05-18T09:03:03Z")

</div>

Thank you for this!  
That's kind of how I had it set up before trying to play with comprehensions in it 😛  
It's good to be led away from barking up the wrong tree.  
New dictionary stepping options is nice to have - I shall bark up that tree instead.

@cmallonee I've never had to use a project name for a named query. There are several of them in the projects I have - trying to put the project name as an argument in the named query call actually seems to upset it... 🤷
