# Named Queries in script escape tag paths wrong

**URL:** <https://forum.inductiveautomation.com/t/named-queries-in-script-escape-tag-paths-wrong/48603>\
**Category:** Ignition\
**Created:** [July 8, 2021, 12:26pm UTC](https://forum.inductiveautomation.com/t/named-queries-in-script-escape-tag-paths-wrong/48603 "2021-07-08T12:26:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![r.p](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@r.p](https://forum.inductiveautomation.com/u/r.p)\
**Post date:** [July 8, 2021, 12:26pm UTC](https://forum.inductiveautomation.com/t/named-queries-in-script-escape-tag-paths-wrong/48603/1 "2021-07-08T12:26:40Z")

</div>

How is escaping treated in Perspective scripting?  
I have the following namedQuery in a script:

> named\_query = "system/tags/getTagPath"  
> parameters = {"tagPath": "/my/tag\_path/"}  
> result = system.db.runNamedQuery(named\_query, parameters)  
> if not result or not result.getRowCount():  
> output = "no result"  
> else:  
> output = "result"

but still when passing a tag path with slashes the input is escaped and no result is returned.

 ![query](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/d/1d65364b4f83ed3487c7bd7fb443f398e4de84c9.png)

I have enabled the option “queryString” in order to avoid automatic escaping of the tagPath input but still no result is returned even though the same input in the Testing tab returns correct results.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 8, 2021, 1:57pm UTC](https://forum.inductiveautomation.com/t/named-queries-in-script-escape-tag-paths-wrong/48603/2 "2021-07-08T13:57:44Z")

</div>

Set the parameter back to a value. In python, the `/` character is escaped by the `/` character. Try using

```auto
parameters = {"tagPath":"//my//tag_path//"}

```

**EDIT:**  
See below the correction by @zacht on the escape character

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [July 8, 2021, 2:18pm UTC](https://forum.inductiveautomation.com/t/named-queries-in-script-escape-tag-paths-wrong/48603/3 "2021-07-08T14:18:55Z")

</div>

Its slashes the other way `(\)` that are escape characters in python.

> [@r.p](#):
>
> the input is escaped

How do you know this?

What's your query (why are you passing a tagpath into SQL)? Does the query work with this input in the testing tab?

---

<div class="post-metadata">

**Author:** ![r.p](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@r.p](https://forum.inductiveautomation.com/u/r.p)\
**Post date:** [July 8, 2021, 4:26pm UTC](https://forum.inductiveautomation.com/t/named-queries-in-script-escape-tag-paths-wrong/48603/4 "2021-07-08T16:26:18Z")

</div>

I have a strong feeling it’s an escape problem because I had a similar problem solved ([named-queries-syntax-error-when-passing-tag-path-strings](http://forum.inductiveautomation.com/t/named-queries-syntax-error-when-passing-tag-path-strings/48267).

My query is: SELECT NiceName FROM my\_tags\_mapping WHERE TagPath = {tagPath}

and the tag path can be anything like “[default]/Edulent/Giroste/Giroste1\_Met01EnergyImm”

and yes the query works ok in the testing tab as is.

---

<div class="post-metadata">

**Author:** ![r.p](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@r.p](https://forum.inductiveautomation.com/u/r.p)\
**Post date:** [July 8, 2021, 4:27pm UTC](https://forum.inductiveautomation.com/t/named-queries-in-script-escape-tag-paths-wrong/48603/5 "2021-07-08T16:27:20Z")

</div>

> [@zacht](#):
>
> ther way `(\)` that are escape characters in python.
> 
> > [@r.p](#):
> >
> > the input is escaped
> 
> How do you know this?

I tried with a

> parameters = {"tagPath": tagPath.replace("/", "/")}

but it doesn't work.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 8, 2021, 5:08pm UTC](https://forum.inductiveautomation.com/t/named-queries-in-script-escape-tag-paths-wrong/48603/6 "2021-07-08T17:08:17Z")

</div>

try using

```auto
parameters = {"tagPath":r"/my/tag_path/"}

```

and make sure you don’t use curly quotes, use straight quotes. Several of your snippets have curly quotes in them.

---

<div class="post-metadata">

**Author:** ![r.p](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@r.p](https://forum.inductiveautomation.com/u/r.p)\
**Post date:** [July 9, 2021, 9:24am UTC](https://forum.inductiveautomation.com/t/named-queries-in-script-escape-tag-paths-wrong/48603/7 "2021-07-09T09:24:56Z")

</div>

ok the tagPath works with parameter type Value (QueryString is not necessary) and by passing the parameter as is thus:

> named\_query = "my/named/query"  
> tagPath = "{}".format(tagPath)  
> parameters = {"tagPath": tagPath}

the problem seems to be the table name, so if you pass the table name such as:

> named\_query = "my/named/query"  
> tagPath = "{}".format(tagPath)  
> parameters = {"tableName": mytable\_name, "tagPath": tagPath}

and the query is something like:

> SELECT \* FROM :tableName where TagId = :tagPath

the query breaks, you need to hard wire the name of the table such as:

> SELECT \* FROM myTable where TagId = :tagPath

apparently the table name is treated differently from other fields in the query

PS: ignore curly quotes, these are added by the CMS of this forum but they are normal quotes indeed

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 9, 2021, 12:31pm UTC](https://forum.inductiveautomation.com/t/named-queries-in-script-escape-tag-paths-wrong/48603/8 "2021-07-09T12:31:45Z")

</div>

As far as I know, you cannot make the table name dynamic in a named query. This would have been identified much quicker if you would have included your query in the original post. Anyway, you would be better served by [system.db.runPrepQuery()](https://docs.inductiveautomation.com/display/DOC81/system.db.runPrepQuery).

```auto
query = " select * from %s where TagId = ?" % (tableName)
results = system.db.runPrepQuery(query, [tagPath], database = <myDBName>)
               

```

Note that the database name is required if you run this at the gateway scope. It can be omitted if you run it at the project level and you want to use the project’s default DB connection.

---

<div class="post-metadata">

**Author:** ![r.p](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@r.p](https://forum.inductiveautomation.com/u/r.p)\
**Post date:** [July 9, 2021, 2:21pm UTC](https://forum.inductiveautomation.com/t/named-queries-in-script-escape-tag-paths-wrong/48603/9 "2021-07-09T14:21:11Z")

</div>

> [@dkhayes117](#):
>
> ow, you cannot make the table name dynamic in a named query. This would have been identified much quicker if you would have included your query in the original post. Anyway, you would be better served by [system.db.runPrepQuery()](https://docs.inductiveautomation.com/display/DOC81/system.db.runPrepQuery).

yes but the problem affected several complex queries and I didn't see some also had the table name as parameter, anyway I found this has been solved in the forum here: [How to pass a table name parameter to named query?](http://forum.inductiveautomation.com/t/how-to-pass-a-table-name-parameter-to-named-query/32689)

Marking as solved, thank you

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [July 9, 2021, 2:49pm UTC](https://forum.inductiveautomation.com/t/named-queries-in-script-escape-tag-paths-wrong/48603/10 "2021-07-09T14:49:34Z")

</div>

You can make the table name dynamic in a named query. You have to set the table name parameter type to `QueryString`.

When a parameter is set to type `QueryString`, you cannot reference the parameter with a colon in your query, you must enclose it in curly braces instead.

> [@r.p](#):
>
> SELECT \* FROM :tableName where TagId = :tagPath

The above query should be (assuming you have `tableName` parameter properly set to type `QueryString`):

```auto
SELECT * FROM {tableName} WHERE TagId = :tagPath

```

BIG warning here! Be very careful with how you handle `QueryString` parameters. You should NOT allow end users to directly write to these parameters in the client as you are opening yourself up to SQL injection attacks.
