On re-reading it appears that you are trying to create a named query in a script. That's not how it works. You create the named query in the Project Browser section labelled Named Query. You then use it in your script in the manner of, results = system.db.runNamedQuery('WasteBarrelDesc')
If you're binding a perspective table to a query, you probably don't need to use runNamedQuery. Just use a query binding.
Don't use scripting unless necessary. Ignition has a lot a built-in ways of doing things efficiently, use them before resorting to custom methods.
I am using the namedQuery in python. I have come up with something like this:
# START Getting Barrel Number based on TID
barrelNum_namedQuery = system.db.runNamedQuery('WasteBarrelDesc')
dataset = barrelNum_namedQuery
for i in range(dataset.getRowCount()):
if dataset.getValueAt(i, 'TID') == TID:
barrelNum = dataset.getValueAt(i, 'BarrelNum')
break
else:
print("No matching row found.")
# END Getting Barrel Number based on TID
I am making it execute via an "on change" script of a tag. Is this an OK way of doing it?
Where do you want to display that barrel num ?
I'm going to suppose it's on a label, in a view.
Create a new named query.
Set it to be a SCALAR query.
Add a parameter of type string, call it TID.
Write the query so it looks like this
SELECT BarrelNum
FROM your_barrel_table
WHERE TID = :TID
In the view where you want to display it, put a label.
Put a query binding on that label, and use your new named query.
Pass the TID to look for as the parameter to the query.
If you REALLY want to use your already existing named query (but don't), add a custom property on the label, put a query binding on that property, use your named query.
Now put a structure binding on the label, and add the custom property and the TID source.
Add an expression transform and use lookup to retrieve your barrel num.
No scripting needed anywhere, no change scripts, no touching tags.