# Creating a sting using a row from a name queries

**URL:** <https://forum.inductiveautomation.com/t/creating-a-sting-using-a-row-from-a-name-queries/70983>\
**Category:** Ignition\
**Created:** [March 13, 2023, 6:34pm UTC](https://forum.inductiveautomation.com/t/creating-a-sting-using-a-row-from-a-name-queries/70983 "2023-03-13T18:34:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joshua\_Hoekstra](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Joshua\_Hoekstra](https://forum.inductiveautomation.com/u/Joshua_Hoekstra)\
**Post date:** [March 13, 2023, 6:34pm UTC](https://forum.inductiveautomation.com/t/creating-a-sting-using-a-row-from-a-name-queries/70983/1 "2023-03-13T18:34:52Z")

</div>

Looking for an efficient way to script from a name query to build a string from a row using all the columns in the row separated by columns. I want to write it to a tag. I am starting off using the db.runNamedQuery to run the query then system. tag.writeblocking to the tag. I just can't find much information on how to build the data collection part in the middle to build the string. Appreciate any help I can get!

---

<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:** [March 13, 2023, 7:01pm UTC](https://forum.inductiveautomation.com/t/creating-a-sting-using-a-row-from-a-name-queries/70983/2 "2023-03-13T19:01:38Z")

</div>

There are many ways to accomplish this, the question I have is why? Generally speaking you want to leave values as there base types and do any conversion to string as a last chance operation.

So, why not keep the data as a dataset or JSON object? Why write it to a tag? Does it truly need to be gateway global information?

Where are you executing this script? Vision, perspective, Gateway event, etc....

---

<div class="post-metadata">

**Author:** ![Joshua\_Hoekstra](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Joshua\_Hoekstra](https://forum.inductiveautomation.com/u/Joshua_Hoekstra)\
**Post date:** [March 13, 2023, 7:07pm UTC](https://forum.inductiveautomation.com/t/creating-a-sting-using-a-row-from-a-name-queries/70983/3 "2023-03-13T19:07:02Z")

</div>

I was looking to to grab an order from the data base which i send to my PLC so i can use to tell my machine what parts to run. I was going to have to PLC send tag to run the query then move the order in string and send it back to my plc. I am very green so i am looking for the best direction to go.

---

<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:** [March 13, 2023, 7:14pm UTC](https://forum.inductiveautomation.com/t/creating-a-sting-using-a-row-from-a-name-queries/70983/4 "2023-03-13T19:14:44Z")

</div>

Do you have the SQL Bridge module? Because transaction groups are made for this process.

If not, then consider using Gateway Tag Events. You can then use `system.db.runNamedQuery()` to execute the query. Then process the returned data and use `system.opc.writeValues()` to write the values out directly to the PLC.

Keep the values in the data type that they are.

Is the PLC expecting a concatenated string? Or multiple values?

---

<div class="post-metadata">

**Author:** ![Joshua\_Hoekstra](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Joshua\_Hoekstra](https://forum.inductiveautomation.com/u/Joshua_Hoekstra)\
**Post date:** [March 14, 2023, 12:58pm UTC](https://forum.inductiveautomation.com/t/creating-a-sting-using-a-row-from-a-name-queries/70983/5 "2023-03-14T12:58:16Z")

</div>

Yes, i will be using the PLC to pull values out of the string to run certain parts of the machine. When I run the name query i have 1 row and 10 column so i am hoping i can do the system.db.runNamequery(). then pull all the columns together out of the row and then send it to tag. I have the SQL bridge module but i can only seem to bring the columns in separate not together.

---

<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:** [March 14, 2023, 3:05pm UTC](https://forum.inductiveautomation.com/t/creating-a-sting-using-a-row-from-a-name-queries/70983/6 "2023-03-14T15:05:46Z")

</div>

> [@Joshua\_Hoekstra](#):
>
> i will be using the PLC to pull values out of the string to run certain parts of the machine

Is this your choice or something you are stuck with? IMO, PLC's aren't the greatest at string manipulation, if you can keep the parts separate from the start, that would be my recommendation.

If not then you will have to build the string up and then write it to the tag.

```python
dsReturn = system.db.runNamedQuery('PathToYourQuery',{'param1':param1})

#getColumnNames from dataset and pull values from query results to build string
#I have chosen to create a dictionary and then encode that as JSON before writing to the tag.
#This should give you a consitent model for manipulating the string.

jsonOut = system.util.jsonEncode({col:dsReturn.getValueAt(col,0) for col in dsReturn.columnNames})

#if you truely need this information elsewhere on the gateway.
system.tag.writeBlocking(['[~provider~]tagPath',[jsonOut])

#if you only need the information in the device then you can use
#system.opc.writeValue('OPC Server', 'OPC Item Path',jsonOut)

```

---

<div class="post-metadata">

**Author:** ![Joshua\_Hoekstra](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@Joshua\_Hoekstra](https://forum.inductiveautomation.com/u/Joshua_Hoekstra)\
**Post date:** [March 15, 2023, 1:03pm UTC](https://forum.inductiveautomation.com/t/creating-a-sting-using-a-row-from-a-name-queries/70983/7 "2023-03-15T13:03:49Z")

</div>

I am stuck with this, and i walked into this by pervous contorls guy that promised it so now i am trying to figure out how to make it all work
