# Using a tag value in a SQL query?

**URL:** <https://forum.inductiveautomation.com/t/using-a-tag-value-in-a-sql-query/67250>\
**Category:** Ignition\
**Created:** [December 5, 2022, 9:57pm UTC](https://forum.inductiveautomation.com/t/using-a-tag-value-in-a-sql-query/67250 "2022-12-05T21:57:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rahman.tarek](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@rahman.tarek](https://forum.inductiveautomation.com/u/rahman.tarek)\
**Post date:** [December 5, 2022, 9:57pm UTC](https://forum.inductiveautomation.com/t/using-a-tag-value-in-a-sql-query/67250/1 "2022-12-05T21:57:55Z")

</div>

Hi there. i have a UDT that contains an expression tag and a query tag. i want to use the value of the expression tag in the query itself.

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

This is my expression tag  
split({PathtoTag},"/")[7,0]

This is my query

SELECT TOP 1 (operationNumber) as OpNum  
FROM CT\_Part\_Info  
WHERE workstation = {tagvalue}  
ORDER BY id DESC

I want to use the value of the expression tag and have my workstation= to that. but it does not work or seems is not possible. any recommendations?

---

<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:** [December 5, 2022, 11:14pm UTC](https://forum.inductiveautomation.com/t/using-a-tag-value-in-a-sql-query/67250/2 "2022-12-05T23:14:53Z")

</div>

Tips:

1. When posting code, select it and press the \</\> button to format it. It will preserve indentation and do some syntax highlighting for you.
2. It looks like you're working on a _Vision_ application. If you edit your question title and add in the _Vision_ tag it may help attract the Vision experts.

---

<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:** [December 6, 2022, 2:02am UTC](https://forum.inductiveautomation.com/t/using-a-tag-value-in-a-sql-query/67250/3 "2022-12-06T02:02:10Z")

</div>

Pretty sure this isn't possible to do. You can't reference tags from tag property bindings, you can only reference the specific props in the list. You'll just have to live with using named queries instead

---

<div class="post-metadata">

**Author:** ![user77](https://avatars.discourse-cdn.com/v4/letter/u/a183cd/32.png) [@user77](https://forum.inductiveautomation.com/u/user77)\
**Post date:** [December 6, 2022, 3:09am UTC](https://forum.inductiveautomation.com/t/using-a-tag-value-in-a-sql-query/67250/4 "2022-12-06T03:09:23Z")

</div>

You are missing your tag reference in the screenshot. You also have no space before WHERE and FROM which will cause the query to fail.

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/6/865db3f428b48bdf01e4964e0eac8d32686f6def.png)

Otherwise, it should work if your tag path and data type is correct.

There are other means to achieve what you are looking for and for security reasons it is best practice to create Named Queries

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/b/3b451ae781e329eb2a3206df695384bd9b7193de.png)  
[https://docs.inductiveautomation.com/display/DOC81/Named+Queries](https://docs.inductiveautomation.com/display/DOC81/Named+Queries)
