# Query coding with Dynamic Column naming

**URL:** <https://forum.inductiveautomation.com/t/query-coding-with-dynamic-column-naming/110471>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [September 8, 2025, 11:27pm UTC](https://forum.inductiveautomation.com/t/query-coding-with-dynamic-column-naming/110471 "2025-09-08T23:27:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bill\_Huang](https://avatars.discourse-cdn.com/v4/letter/b/b4bc9f/32.png) [@Bill\_Huang](https://forum.inductiveautomation.com/u/Bill_Huang)\
**Post date:** [September 8, 2025, 11:27pm UTC](https://forum.inductiveautomation.com/t/query-coding-with-dynamic-column-naming/110471/1 "2025-09-08T23:27:50Z")

</div>

Hi guys,

I’m trying to get the contents in a certain column referring to the name of the row in a table. My code is following:

```python
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	Target_Column_SQL = "[.]Station_ID"
	Name = "[.]Name"
	Trainer = "[.]Trainner_Log_In"
	Level = "[.]Training_Level"
	sourceTagPath = "[.]Badge"
	database = 'DW_SCADA_MSSQL'
	
	badgeRead = system.tag.readBlocking([sourceTagPath])[0].value
	target = system.tag.readBlocking([Target_Column_SQL])[0].value
	
	ReadResult = []
	
	if currentValue.value != "0":
		query = "SELECT Badge, employee_name, [{0}]] FROM Training_Matrix WHERE Badge = ?".format(target)
		ReadResult = system.db.runPrepQuery(query, [badgeRead], database)
		
	if ReadResult:
		system.tag.writeBlocking([Name], [ReadResult[0][1]])
		system.tag.writeBlocking([Level], [ReadResult[0][2]])

		if ReadResult[0][1] in ('Khuc, Hoa The', 'Osorno, Hector', 'Imafidon-Taiwo, Shogbolu', 'Janapareddy, Sreedhar'):
			system.tag.writeBlocking([Trainer], [True])
	else:
		system.tag.writeBlocking([Name], ["User Not Found"])

		system.tag.writeBlocking([Level], [""])

```

The script attached to a child tag to my UDT as following:

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

For some reason the script would not work as I expected.

But when I remove the dynamic element [{0}] and .format(target) then replace with solid name “WL\_OP10\_30“ in the following code, the script works.

```python
query = "SELECT Badge, employee_name, [{0}]] FROM Training_Matrix WHERE Badge = ?".format(target)

```

```python
query = "SELECT Badge, employee_name, WL_OP10_30 FROM Training_Matrix WHERE Badge = ?"

```

Can anyone give me some clue?

\*\*Sorry for the confusing if the layout doesn't make sense.

---

<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:** [September 8, 2025, 11:39pm UTC](https://forum.inductiveautomation.com/t/query-coding-with-dynamic-column-naming/110471/2 "2025-09-08T23:39:26Z")

</div>

I don't understand the syntax, but have you an error in `[{0}]]`?  
The square bracket counts are different.

---

<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:** [September 9, 2025, 1:18am UTC](https://forum.inductiveautomation.com/t/query-coding-with-dynamic-column-naming/110471/3 "2025-09-09T01:18:21Z")

</div>

It is unsafe to run database queries in tag `valueChange()` event scripts. (Only updates through the S&F subsystem are safe.) Move all of this code to a gateway tag change event script (project, not on a tag).

Anything that might cause a script to take more than a handful of milliseconds risks locking up all tag event scripting.

---

<div class="post-metadata">

**Author:** ![Bill\_Huang](https://avatars.discourse-cdn.com/v4/letter/b/b4bc9f/32.png) [@Bill\_Huang](https://forum.inductiveautomation.com/u/Bill_Huang)\
**Post date:** [September 9, 2025, 1:37am UTC](https://forum.inductiveautomation.com/t/query-coding-with-dynamic-column-naming/110471/4 "2025-09-09T01:37:18Z")

</div>

Sorry that was a mistake. I have removed it but nothing changes

---

<div class="post-metadata">

**Author:** ![Bill\_Huang](https://avatars.discourse-cdn.com/v4/letter/b/b4bc9f/32.png) [@Bill\_Huang](https://forum.inductiveautomation.com/u/Bill_Huang)\
**Post date:** [September 9, 2025, 1:42am UTC](https://forum.inductiveautomation.com/t/query-coding-with-dynamic-column-naming/110471/5 "2025-09-09T01:42:31Z")

</div>

Thank you for the reply, it’s good to know. I have moved all these code to script gatway event, but it still doesn't work.

Btw, I have another tag with the same code in tag valuechange event, and it works fine. The different between these 2 tags are the data type. The one with script working is an individual OPC tag, while the one has the problem is one of the child tag of a UDT which contains a few tags in different data type

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [September 9, 2025, 2:41am UTC](https://forum.inductiveautomation.com/t/query-coding-with-dynamic-column-naming/110471/6 "2025-09-09T02:41:24Z")

</div>

Unrelated, but you also should not be reading and writing to single tags sequentially with multiple calls to system.tag.\*; multiple reads/writes should always be done in a single call by supplying a list, for efficiency, otherwise you're building artificial scalability limits into your application

---

<div class="post-metadata">

**Author:** ![David\_Stone](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/david_stone/32/79749_2.png) [@David\_Stone](https://forum.inductiveautomation.com/u/David_Stone)\
**Post date:** [September 9, 2025, 8:54am UTC](https://forum.inductiveautomation.com/t/query-coding-with-dynamic-column-naming/110471/7 "2025-09-09T08:54:26Z")

</div>

Can you share the error from the logs?
