# Named query returns null for some columns only if run in a script

**URL:** <https://forum.inductiveautomation.com/t/named-query-returns-null-for-some-columns-only-if-run-in-a-script/59231>\
**Category:** Ignition\
**Created:** [May 5, 2022, 3:03am UTC](https://forum.inductiveautomation.com/t/named-query-returns-null-for-some-columns-only-if-run-in-a-script/59231 "2022-05-05T03:03:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bebel](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@bebel](https://forum.inductiveautomation.com/u/bebel)\
**Post date:** [May 5, 2022, 3:03am UTC](https://forum.inductiveautomation.com/t/named-query-returns-null-for-some-columns-only-if-run-in-a-script/59231/1 "2022-05-05T03:03:00Z")

</div>

I have a named query which returns valid values for all columns in the named query editor. If I crated a binding on a table as a named query binding, it also works fine. If I call the named query in a script using system.db.runNamedQuery, the last two columns return null. The first 7 columns return the same values. I have verified that the parameters are the same in both methods, even though I had no reason to suspect them (since 7 of the 9 column values match.)

---

<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, 2022, 3:11am UTC](https://forum.inductiveautomation.com/t/named-query-returns-null-for-some-columns-only-if-run-in-a-script/59231/2 "2022-05-05T03:11:06Z")

</div>

Very unlikely anyone can tell you what, if anything, is wrong with your script without seeing it.

Please post it using the preformatted text (`</>`)

---

<div class="post-metadata">

**Author:** ![hayden](https://avatars.discourse-cdn.com/v4/letter/h/91b2a8/32.png) [@hayden](https://forum.inductiveautomation.com/u/hayden)\
**Post date:** [May 5, 2022, 3:14am UTC](https://forum.inductiveautomation.com/t/named-query-returns-null-for-some-columns-only-if-run-in-a-script/59231/3 "2022-05-05T03:14:38Z")

</div>

Without the script it’s hard to tell, my only guess is that no values return for those columns, named query will return an empty list while the python might see it as a None value and return a null.

Maybe modify the script to check for None values and fill it with a zero or some place holder string

---

<div class="post-metadata">

**Author:** ![bebel](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@bebel](https://forum.inductiveautomation.com/u/bebel)\
**Post date:** [May 5, 2022, 1:05pm UTC](https://forum.inductiveautomation.com/t/named-query-returns-null-for-some-columns-only-if-run-in-a-script/59231/4 "2022-05-05T13:05:09Z")

</div>

I didn’t include the script and other methods because I was really expecting this to be some known bug, not a problem with my script. I’ll include all of my supporting evidence though to help figure if that is true, or if I have made some mistake.  
FYI, this is an Oracle DB and the two columns that I am having an issue with are aggregate functions.  
From the Named Query editor, this is the SELECT portion of the query:  
SELECT B.BUSINESS\_UNIT, C.DESCR60, C.UNIT\_MEASURE\_STD, D.INV\_ITEM\_HEIGHT, D.INV\_ITEM\_WIDTH, D.INV\_ITEM\_LENGTH, B.ITEM\_FIELD\_N12\_A, SUM( E.TL\_COST) AS COST\_1, SUM( E.LL\_COST) AS COST\_2  
It is COST\_1 and COST\_2 that are the issue.  
When running from the editor:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/5/056924651d0f0fbf83fdbd5b5792692a2e8403cb.png)  
In the script:

```auto
	params = {'database':self.database, 'BU':self.BU, 'ItemID':CompID, 'version':self.version}
	details = system.db.runNamedQuery('AMP_IG_COMPONENTS DETAILS',params)
	self.testDS = details
	print params
	print details.getValueAt(0,'COST_1')
	print details.getValueAt(0,7) # just in case it doesn't like the col name
	return details

```

The printed reultts from the output console:  
{‘ItemID’: u’R907022’, ‘version’: u’202201’, ‘database’: u’Peoplesoft\_TST’, ‘BU’: u’EGRMI’}  
None  
None

And the actual query results from the scripted version placed in a dataset parameter:

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

I think this shows that I am feeding the same parameter values using both methods but getting different results.

---

<div class="post-metadata">

**Author:** ![bebel](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@bebel](https://forum.inductiveautomation.com/u/bebel)\
**Post date:** [May 5, 2022, 1:12pm UTC](https://forum.inductiveautomation.com/t/named-query-returns-null-for-some-columns-only-if-run-in-a-script/59231/5 "2022-05-05T13:12:46Z")

</div>

Also, I tried removing the ‘AS COST\_1’ part but that only changed the col name in the result (as expected). The values were still null.  
version is: 8.1.9

---

<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:** [May 5, 2022, 1:39pm UTC](https://forum.inductiveautomation.com/t/named-query-returns-null-for-some-columns-only-if-run-in-a-script/59231/6 "2022-05-05T13:39:43Z")

</div>

Check case of `Version` versus `version`.

---

<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, 2022, 1:51pm UTC](https://forum.inductiveautomation.com/t/named-query-returns-null-for-some-columns-only-if-run-in-a-script/59231/7 "2022-05-05T13:51:22Z")

</div>

You didn’t include the WHERE and GROUP BY statements for the QUERY.

The named query calls for 4 parameters, but you didn’t show where and how those are used in the query itself.

Also, when supplying the Keys for the params dictionary the case must match what was used in the Named query.

I suspect that the `Version` parameter has some effect on the grouping for the aggregate functions and since the script (at least the version you provided) is not suppling a `Version` key then that parameter is effectively null.

---

<div class="post-metadata">

**Author:** ![bebel](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@bebel](https://forum.inductiveautomation.com/u/bebel)\
**Post date:** [May 5, 2022, 3:06pm UTC](https://forum.inductiveautomation.com/t/named-query-returns-null-for-some-columns-only-if-run-in-a-script/59231/8 "2022-05-05T15:06:48Z")

</div>

You are correct. I supplied version instead of Version. Thank-you.
