# Swap Named Query in Expression

**URL:** <https://forum.inductiveautomation.com/t/swap-named-query-in-expression/62587>\
**Category:** Ignition\
**Tags:** ignition81, vision\
**Created:** [August 2, 2022, 2:00pm UTC](https://forum.inductiveautomation.com/t/swap-named-query-in-expression/62587 "2022-08-02T14:00:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwilliams](https://avatars.discourse-cdn.com/v4/letter/j/c2a13f/32.png) [@jwilliams](https://forum.inductiveautomation.com/u/jwilliams)\
**Post date:** [August 2, 2022, 2:00pm UTC](https://forum.inductiveautomation.com/t/swap-named-query-in-expression/62587/1 "2022-08-02T14:00:36Z")

</div>

I am trying to swap between two different named queries for a dataset based on a tag value.  
So I have a Named Query binding on a vision screen that populates a dataset and I need to be able to change which named query is running based on a tag value. I have attempted to use an expression to accomplish this however I havent had any luck getting it to work and cant seem to find any documentation on if it is even possible, I am leaning towards it is not but I wanted to make a post and ask in case I have missed something.  
I know it would be possible to do this through scripting like this however that isnt an option on the binding (InternalProperty binding):

```auto
system.db.runNamedQuery("QueryName", {params})

```

and this is the expression version I have tried but it always returns that an illegal character is in the expression as the dot between _system_ and _db_

```auto
if({[default]tag} = -2, system.db.runNamedQuery("Query1",{}), system.db.RunNamedQuery("Query2",{"param":uid}))

```

---

<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:** [August 2, 2022, 2:30pm UTC](https://forum.inductiveautomation.com/t/swap-named-query-in-expression/62587/2 "2022-08-02T14:30:49Z")

</div>

> [@jwilliams](#):
>
> I know it would be possible to do this through scripting like this however that isnt an option on the binding (InternalProperty binding):

I don't understand why it wouldn't be possible. If you have `{[default]tag}` as a root container property (or just as a custom property somewhere) you can run propertyChange script on it and use that.

```auto
if event.propertyName == 'myTag':
    if event.newValue == -2:
        system.db.runNamedQuery("Query1",{})
    else: 
        system.db.RunNamedQuery("Query2",{"param":uid})

```

Though you could also handle this in a gateway tag change event. When the tag you care about changes you can do your logic there and assign the result to a dataset tag.

I suppose you could probably do this in a run script. Maybe something like

```auto
def runAppropriateNQ(tagValue, uid):
    if tagValue == -2:
        return system.db.runNamedQuery("Query1",{})
    else:
        system.db.runNamedQuery("Query2",{"param":uid})

```

and then your expression would be  
`runScript('someProjLibrary.runAppropriateNQ',0,{SomePath.To.TagValue}, {SomePath.To.uid})`

and this would return an appropriate dataset. However to use runscript you can't use keyword arguments so I know that your uid doesn't matter for your first named query - you still need to feed _something_ in.

---

<div class="post-metadata">

**Author:** ![jwilliams](https://avatars.discourse-cdn.com/v4/letter/j/c2a13f/32.png) [@jwilliams](https://forum.inductiveautomation.com/u/jwilliams)\
**Post date:** [August 2, 2022, 2:51pm UTC](https://forum.inductiveautomation.com/t/swap-named-query-in-expression/62587/3 "2022-08-02T14:51:50Z")

</div>

> [@bkarabinchak.psi](#):
>
> I don’t understand why it wouldn’t be possible. If you have `{[default]tag}` as a root container property (or just as a custom property somewhere) you can run propertyChange script on it and use that.
> 
> ```python
> if event.propertyName == 'myTag':
> if event.newValue == -2:
> system.db.runNamedQuery("Query1",{})
> else: 
> system.db.RunNamedQuery("Query2",{"param":uid})
> 
> ```

I see where you're going with this but I dont understand how I would pass that to my internal property. Its a Named Query binding and as far as I can tell there is no way of binding the named query in the script into the Named Query path of the Internal Property.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/6/8683180e14673a2eeff4fdba5b29b7bad8f3621e.png)

My script looks something like this currently:

```python
if event.propertyName == 'UserChoice':
	if event.newValue == -2:
        system.db.runNamedQuery("Query1",{})
		do stuff here
			
		for row in jobs:
			if row['jnum'] == jid:
				do more stuff here
				break
	else:
		system.db.runNamedQuery("Query2",{params here})
		do stuff here
				
		for row in jobs:
			if row['jnum'] == jid:
				do more here
				break

```

---

<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:** [August 2, 2022, 2:58pm UTC](https://forum.inductiveautomation.com/t/swap-named-query-in-expression/62587/4 "2022-08-02T14:58:24Z")

</div>

Oh ok this is inside a template. Shouldn’t be a problem as the propertyChange works on them identically.

I don’t see why you couldn’t do something like

```auto
if event.propertyName == 'UserChoice':
	if event.newValue == -2:
        jobs = system.db.runNamedQuery("Query1",{})
		do stuff here
			
		for row in jobs:
			if row['jnum'] == jid:
				do more stuff here
				break
	else:
		jobs = system.db.runNamedQuery("Query2",{params here})
		do stuff here
				
		for row in jobs:
			if row['jnum'] == jid:
				do more here
				break
    
    event.source.IncompleteJobs = jobs

```

But this would mean there should be no binding on IncompleteJobs.

---

<div class="post-metadata">

**Author:** ![jwilliams](https://avatars.discourse-cdn.com/v4/letter/j/c2a13f/32.png) [@jwilliams](https://forum.inductiveautomation.com/u/jwilliams)\
**Post date:** [August 2, 2022, 3:02pm UTC](https://forum.inductiveautomation.com/t/swap-named-query-in-expression/62587/5 "2022-08-02T15:02:07Z")

</div>

> [@bkarabinchak.psi](#):
>
> But this would mean there should be no binding on IncompleteJobs.

So its possible to parse through a dataset table without it actually being visible on any binding or table component? It can just exist as code like that?

Also if it matters here is more of the code, I had trimmed it to make it easier as I wasnt sure it mattered in the post but as soon as you put the _jobs =_ I noticed a possible issue:

```python
if event.propertyName == 'UserChoice':
	if event.newValue == -2:
		system.db.runNamedQuery("Query1",{})
		jid = event.source.JobNum
		jobs = system.dataset.toPyDataSet(event.source.IncompleteJobs)
			
		for row in jobs:
			if row['jnum'] == jid:
				do not important things here
				break
	else:
		system.db.runNamedQuery("Query2",{params here})
		jid = event.source.StationNum
		jobs = system.dataset.toPyDataSet(event.source.IncompleteJobs)
		do not important things here
				
		for row in jobs:
			if row['jnum'] == jid:
				do not important things here
				break

```

---

<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:** [August 2, 2022, 3:11pm UTC](https://forum.inductiveautomation.com/t/swap-named-query-in-expression/62587/6 "2022-08-02T15:11:10Z")

</div>

You can just have a raw dataset as a custom property and not do anything with it.

But if you want to show it on table you can just bind to the internal property -

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/4/b4e96e038fd06489a99f8a7b99b1eb8d300e9e91.png)

One thing with your code though is you never assign the dataset to a variable so you can’t ever assign it to your internal property or parameter or whatever.

So instead of

```auto
system.db.runNamedQuery("Query2",{params})

```

It should be

```auto
# ds = dataset
ds = system.db.runNamedQuery("Query2",{params})
#Do stuff with ds
event.source.IncompleteJobs = ds

```

and then you can parse the dataset variable ds, modify it, update, whatever, before assigning it to a property (presumably the same property your table data is bound too - I am assuming the IncompleteJobs property).

---

<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:** [August 2, 2022, 3:14pm UTC](https://forum.inductiveautomation.com/t/swap-named-query-in-expression/62587/7 "2022-08-02T15:14:21Z")

</div>

Just thought of an easier way that may be better for you.

You can also just always query both datasets. Then in your table data expression you figure out which one to use.

So perhaps a two internal properies like

```auto
namedQuery1 - Named Query binding to NamedQuery1 with no params
namedQuery2 - Named Query binding to NamedQuery2 with params

```

and then your table.data could look like

```auto
if(# SOME CONDITION HERE,
{path.to.namedQuery1},
{path.to.namedQuery2})

```

May be slightly inefficient since you’re always calling both named queries regardless of the situation but it does let you stick to doing all of this strictly with properties and expressions.

---

<div class="post-metadata">

**Author:** ![jwilliams](https://avatars.discourse-cdn.com/v4/letter/j/c2a13f/32.png) [@jwilliams](https://forum.inductiveautomation.com/u/jwilliams)\
**Post date:** [August 2, 2022, 3:25pm UTC](https://forum.inductiveautomation.com/t/swap-named-query-in-expression/62587/8 "2022-08-02T15:25:39Z")

</div>

> [@bkarabinchak.psi](#):
>
> Just thought of an easier way that may be better for you.
> 
> You can also just always query both datasets. Then in your table data expression you figure out which one to use.
> 
> So perhaps a two internal properies like
> 
> ```python
> namedQuery1 - Named Query binding to NamedQuery1 with no params
> namedQuery2 - Named Query binding to NamedQuery2 with params
> 
> ```
> 
> and then your table.data could look like
> 
> ```python
> if(# SOME CONDITION HERE,
> {path.to.namedQuery1},
> {path.to.namedQuery2})
> 
> ```

I cannot believe I didn't think of this. It works exactly as I needed!
