# Using tag value in SQL query

**URL:** <https://forum.inductiveautomation.com/t/using-tag-value-in-sql-query/11024>\
**Category:** Ignition\
**Created:** [January 11, 2016, 11:12am UTC](https://forum.inductiveautomation.com/t/using-tag-value-in-sql-query/11024 "2016-01-11T11:12:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![waker1der](https://avatars.discourse-cdn.com/v4/letter/w/dbc845/32.png) [@waker1der](https://forum.inductiveautomation.com/u/waker1der)\
**Post date:** [January 11, 2016, 11:12am UTC](https://forum.inductiveautomation.com/t/using-tag-value-in-sql-query/11024/1 "2016-01-11T11:12:05Z")

</div>

I am running an event based SQl query to time stamp an estop

I need help using the tag name in my script. my python is useless

My script works as follows

system.db.runUpdateQuery(“INSERT INTO LTN4\_ESTOP (t\_stamp, estop\_name, message) VALUE (‘t\_stamp’, ‘tagName’, ‘EStop Active’)”)

I want to use the value of a tag called t\_stamp and the name of my tag and insert it in to my query so that it updates the table with the name of the estop that has been pushed and at what time

I want to update the UDT so this is a generic script for all ESTOP Tags

Thank you in advance

---

<div class="post-metadata">

**Author:** ![waker1der](https://avatars.discourse-cdn.com/v4/letter/w/dbc845/32.png) [@waker1der](https://forum.inductiveautomation.com/u/waker1der)\
**Post date:** [January 11, 2016, 1:28pm UTC](https://forum.inductiveautomation.com/t/using-tag-value-in-sql-query/11024/2 "2016-01-11T13:28:35Z")

</div>

I’ve sorted out the timestamp issue I just need to be able to pass the tag name value in to my SQL query

---

<div class="post-metadata">

**Author:** ![Chris\_Taylor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chris_taylor/32/3315_2.png) [@Chris\_Taylor](https://forum.inductiveautomation.com/u/Chris_Taylor)\
**Post date:** [January 11, 2016, 4:43pm UTC](https://forum.inductiveautomation.com/t/using-tag-value-in-sql-query/11024/3 "2016-01-11T16:43:33Z")

</div>

waker1der

take a look a the “system.db.runPrepUpdate command” [https://docs.inductiveautomation.com:8443/display/DOC/system.db.runPrepUpdate](https://docs.inductiveautomation.com:8443/display/DOC/system.db.runPrepUpdate).

its probably what I would use. Take a look and come back if you need help.

don’t forget to use “system.tag.read” or “system.tag.readAll” in your script to get the tag name value

---

<div class="post-metadata">

**Author:** ![Chris\_Taylor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chris_taylor/32/3315_2.png) [@Chris\_Taylor](https://forum.inductiveautomation.com/u/Chris_Taylor)\
**Post date:** [January 11, 2016, 5:02pm UTC](https://forum.inductiveautomation.com/t/using-tag-value-in-sql-query/11024/4 "2016-01-11T17:02:41Z")

</div>

Ah… 😊

I’ve just realised that you actually want the tag name, not value

`tagPath = 'Your Tags Path'
tagName = system.tag.getAttribute(tagPath,"Name")`

system.tag.getAttribute has been depreciated but it still works, I’m not sure of the latest method

---

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://forum.inductiveautomation.com/u/Pat)\
**Post date:** [January 11, 2016, 7:50pm UTC](https://forum.inductiveautomation.com/t/using-tag-value-in-sql-query/11024/5 "2016-01-11T19:50:50Z")

</div>

You could do this in the Tag Value Changed event script

[code]estopMessage = ‘EStop Inactive’  
if currentValue == 1:  
estopMessage = ‘EStop Active’

system.db.runPrepUpdate(“INSERT INTO LTN4\_ESTOP (t\_stamp, estop\_name, message) VALUE (NOW(), ?, ?)”, [tagPath, estopMessage], yourDatabaseConnection)[/code]

---

<div class="post-metadata">

**Author:** ![waker1der](https://avatars.discourse-cdn.com/v4/letter/w/dbc845/32.png) [@waker1der](https://forum.inductiveautomation.com/u/waker1der)\
**Post date:** [January 13, 2016, 8:55am UTC](https://forum.inductiveautomation.com/t/using-tag-value-in-sql-query/11024/6 "2016-01-13T08:55:29Z")

</div>

thanks for the help, i will test today and let you know how i get on

---

<div class="post-metadata">

**Author:** ![waker1der](https://avatars.discourse-cdn.com/v4/letter/w/dbc845/32.png) [@waker1der](https://forum.inductiveautomation.com/u/waker1der)\
**Post date:** [January 13, 2016, 3:59pm UTC](https://forum.inductiveautomation.com/t/using-tag-value-in-sql-query/11024/7 "2016-01-13T15:59:32Z")

</div>

i keep getting an error in my code

i rolled back to a simple update query so that i got to a known good situation

i created a TEST instance of the UDT to frig the value to 1 to test if it wrote to the db, each time I change the value to one I get the little red dot error after the tag and I cant see what the error is on the console, it doesnt write to the db either, the system.tag.write line is proven and has been working for a while

see attached for code

---

<div class="post-metadata">

**Author:** ![Pat](https://avatars.discourse-cdn.com/v4/letter/p/8491ac/32.png) [@Pat](https://forum.inductiveautomation.com/u/Pat)\
**Post date:** [January 13, 2016, 4:33pm UTC](https://forum.inductiveautomation.com/t/using-tag-value-in-sql-query/11024/8 "2016-01-13T16:33:39Z")

</div>

Try adding the database connection name to the runUpdateQuery statement. Since tag event scripts are executed on the Gateway there is no default database connection to use.
