# Named Queries VARCHAR Error

**URL:** <https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307>\
**Category:** Ignition\
**Created:** [May 13, 2024, 1:41pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307 "2024-05-13T13:41:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack\_Jones](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jack\_Jones](https://forum.inductiveautomation.com/u/Jack_Jones)\
**Post date:** [May 13, 2024, 1:41pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/1 "2024-05-13T13:41:08Z")

</div>

I have my code below trying to write into the named query I made

```python
# Retrieve the current values of each variable from their respective tags
paths = [
    "[default]CR Assembly/MDC15/PartSpecificTags/TestTag",
    "[default]CR Assembly/MDC15/PartSpecificTags/StartTime",
    "[default]CR Assembly/MDC15/PartSpecificTags/TempGoodPcs",
    "[default]CR Assembly/MDC15/PartSpecificTags/TempMissingPin",
    "[default]CR Assembly/MDC15/PartSpecificTags/TempPinLoad",
    "[default]CR Assembly/MDC15/PartSpecificTags/TempPlugGage",
    "[default]CR Assembly/MDC15/PartSpecificTags/TempRingGage"
]

data = system.tag.readBlocking(paths)
total_parts = sum(item.value for item in data[2:7])
total_rejects = sum(item.value for item in data[3:7])

params = {
    "PartNumber": data[0].value,
    "StartTime": data[1].value,
    "EndTime": system.date.now(),
    "GoodPcs": data[2].value,
    "MissingPin": data[3].value,
    "PinLoad": data[4].value,
    "PlugGage": data[5].value,
    "RingGage": data[6].value,
    "TotalPartsRan": total_parts,
    "TotalRejects": total_rejects
}

# Run the named query with the parameters
system.db.runNamedQuery("TestQueryForReport", params)`Preformatted text`

```

Here is my Named Query

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/5/25729dbb6263216c7d98bc2f227c24de5fffb4a4.png)  
And the traceback for the error  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/2/d254fd1b3be56b7b5d946f6dae65d485571d72c0.png)

I have only been using ignition for a week so forgive me if this seems simple. But I am planning on putting this script in a tag so that I can log data everytime that data changes. However I have this table made and it says I have incorrect syntax near VARCHAR. I know that when doing the .value on the part number it returns a UNICODE so I have initialized the PartNumber column as a NVARCHAR.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/a/cadba8a64a0b2d7d837f7a0c5594ccdae57ccdce.png)

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![sebastien\_ng](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sebastien_ng/32/55297_2.png) [@sebastien\_ng](https://forum.inductiveautomation.com/u/sebastien_ng)\
**Post date:** [May 13, 2024, 2:11pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/2 "2024-05-13T14:11:22Z")

</div>

print data[0].value  
Maybe you are not getting an varchar value on your script.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 13, 2024, 2:13pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/3 "2024-05-13T14:13:57Z")

</div>

Have you tried inserting using the testing tab?

I'm suspecting something particularly SQLServer-ish, like putting brackets around the column names.

---

<div class="post-metadata">

**Author:** ![Jack\_Jones](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jack\_Jones](https://forum.inductiveautomation.com/u/Jack_Jones)\
**Post date:** [May 13, 2024, 2:13pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/4 "2024-05-13T14:13:57Z")

</div>

This is the output from the read tags and their types

```python
('Value:', u'BN-85139', 'Type:', <type 'unicode'>)
('Value:', Fri May 10 13:00:12 EDT 2024, 'Type:', <type 'java.util.Date'>)
('Value:', 10356, 'Type:', <type 'int'>)
('Value:', 75, 'Type:', <type 'int'>)
('Value:', 107, 'Type:', <type 'int'>)
('Value:', 9, 'Type:', <type 'int'>)
('Value:', 3, 'Type:', <type 'int'>)

```

---

<div class="post-metadata">

**Author:** ![Jack\_Jones](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jack\_Jones](https://forum.inductiveautomation.com/u/Jack_Jones)\
**Post date:** [May 13, 2024, 2:14pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/5 "2024-05-13T14:14:48Z")

</div>

I have some data inserted from tests ran using the testing tab. However, when scripting I get an error.

---

<div class="post-metadata">

**Author:** ![Jack\_Jones](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jack\_Jones](https://forum.inductiveautomation.com/u/Jack_Jones)\
**Post date:** [May 13, 2024, 2:21pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/6 "2024-05-13T14:21:55Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/0/b01d7487274e5dbd98293da80ae2b6159979e48e.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/a/cae673905b6a7eda2f1d7ed1fb9b75ca0437cde4.png)  
When inserting it inserts one row as it should and when executing the select query it reads what was inserted.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 13, 2024, 2:24pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/7 "2024-05-13T14:24:03Z")

</div>

Look in the value names of the query and ensure there is are no leading or trailing spaces. That's hung me up once or thrice.

---

<div class="post-metadata">

**Author:** ![Jack\_Jones](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jack\_Jones](https://forum.inductiveautomation.com/u/Jack_Jones)\
**Post date:** [May 13, 2024, 2:29pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/8 "2024-05-13T14:29:34Z")

</div>

There are no leading or trailing spaces. I will drop the table and recreate it since there is no data to be lost.

```python
CREATE TABLE MDC15 (
    PartNumber NVARCHAR(255),
    StartTime DATE,
    EndTime DATE,
    GoodPcs INT,
    MissingPin INT,
    PinLoad INT,
    PlugGage INT,
    RingGage INT,
    TotalPartsRan INT,
    TotalRejects INT
);

```

Here I went to redo the table with NVARCHAR and when declaring the variable types with the query they usually turn blue. However, NVARCHAR has not. Is there some varaible declaration I could be missing here? When i realized that the tag was returning unicode last I just altered the table instead of drop and create.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/9/590c9a14eceb5c804a230e802f5147bcdacd3156.png)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 13, 2024, 2:30pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/9 "2024-05-13T14:30:45Z")

</div>

If there are no non-Latin charachters in the part number, I'd probably keep it as varchar.

---

<div class="post-metadata">

**Author:** ![Jack\_Jones](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jack\_Jones](https://forum.inductiveautomation.com/u/Jack_Jones)\
**Post date:** [May 13, 2024, 2:32pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/10 "2024-05-13T14:32:08Z")

</div>

That's it. Somehow the code runs now. I just needed to recreate the table. Thank you

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [May 13, 2024, 2:36pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/11 "2024-05-13T14:36:15Z")

</div>

As a note, although you didn't say what version of SQLServer you're using, as of v2019, unicode is supported natively by VARCHAR. 🙂

---

<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:** [May 13, 2024, 2:41pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/12 "2024-05-13T14:41:52Z")

</div>

Tip: Correct grammar would require the column to be named "TotalPartsRun", not "...Ran".

- The machine ran.
- The parts were run.  
🥴

---

<div class="post-metadata">

**Author:** ![Jack\_Jones](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jack\_Jones](https://forum.inductiveautomation.com/u/Jack_Jones)\
**Post date:** [May 13, 2024, 2:48pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/13 "2024-05-13T14:48:54Z")

</div>

I wasn't informed myself. I'm just an intern with a problem. I do not know anything about the gateway. Just designer.

---

<div class="post-metadata">

**Author:** ![Jack\_Jones](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jack\_Jones](https://forum.inductiveautomation.com/u/Jack_Jones)\
**Post date:** [May 13, 2024, 2:49pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/14 "2024-05-13T14:49:41Z")

</div>

This is true, however to keep consistent with variable names I am using the tags defined before my time.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 13, 2024, 6:11pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/15 "2024-05-13T18:11:10Z")

</div>

Gotta give you credit for clean code, posted properly using the formatting tool, with the error encountered and the stack trace, and marking the solution post.  
Well played, I wished this was common enough that it didn't warrant an acknowledgement.  
Welcome aboard.

> [@Jack\_Jones](#):
>
> I am planning on putting this script in a tag so that I can log data everytime that data changes.

Probably not the best idea.  
Depending on what exactly you want to log, you could use the historian, transaction groups, or maybe a gateway event. Putting this kind of operation on tag change scripts (if that was indeed your intention) might give your gateway a serious headache.  
Avoid putting any i/o operations there, or anything more than a very basic computation actually.  
I try as hard as I can to avoid using tag change scripts actually.

---

<div class="post-metadata">

**Author:** ![Jack\_Jones](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jack\_Jones](https://forum.inductiveautomation.com/u/Jack_Jones)\
**Post date:** [May 13, 2024, 6:22pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/16 "2024-05-13T18:22:19Z")

</div>

I have a Part Number that changes maybe twice every 8 hours on 8 Machines. If every machine changed at the same time, it would be simpler, however there is a schedule that changes daily for needs. I only need this data input into someplace that I can pull it out. My goal is to have a report that gives me the data of exactly what happened during the past 24 hours on all machines. Like I said I've only been using ignition for a week and am not sure the best process for this. Any tips would be greatly appreciated

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [May 13, 2024, 6:29pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/17 "2024-05-13T18:29:33Z")

</div>

Rather than a Tag Value Changed Script, use a Gateway Tag Change Script: [Gateway Event Scripts | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-in-ignition/gateway-event-scripts#tag-change-scripts).

---

<div class="post-metadata">

**Author:** ![Jack\_Jones](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@Jack\_Jones](https://forum.inductiveautomation.com/u/Jack_Jones)\
**Post date:** [May 13, 2024, 6:34pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/18 "2024-05-13T18:34:26Z")

</div>

Is the declaration the same`	system.db.runNamedQuery("Jacks_Project","TestQueryForReport", params)`  
or  
`	system.db.runNamedQuery("TestQueryForReport", params)`

I know one is to specify the named query in the project. Does this also include the gateway event scripts? Thank you.

---

<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 13, 2024, 6:39pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/19 "2024-05-13T18:39:44Z")

</div>

You would need the first one since you will be calling the function from a Gateway Scope.

`system.db.runNamedQuery("Jacks_Project", "TestQueryForReport", params)`

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 13, 2024, 8:22pm UTC](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307/20 "2024-05-13T20:22:43Z")

</div>

Don't code in gateway events. Create a function in the script library, and call it from the event.  
Gateway events have weird scoping rules and behavior. Just don't take any risk and call library functions.

[Next page](https://forum.inductiveautomation.com/t/named-queries-varchar-error/90307.md?page=2)
