# system.db.runPrepQuery, with FROM as a argument?

**URL:** <https://forum.inductiveautomation.com/t/system-db-runprepquery-with-from-as-a-argument/41610>\
**Category:** Ignition\
**Created:** [December 17, 2020, 2:25pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepquery-with-from-as-a-argument/41610 "2020-12-17T14:25:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SBL\_MC](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sbl_mc/32/56813_2.png) [@SBL\_MC](https://forum.inductiveautomation.com/u/SBL_MC)\
**Post date:** [December 17, 2020, 2:25pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepquery-with-from-as-a-argument/41610/1 "2020-12-17T14:25:25Z")

</div>

Hi all,

I’ve been trying to loop trough a set of tables in runPrepQuery, but seem to not be allowed to use the FROM statement linked to an argument.

Wont work:  
linje = “linje1\_time”  
system.db.runPrepQuery(“SELECT mProdusert FROM ? WHERE t\_stamp \> DATEADD(mi, -60,GETDATE()) AND t\_stamp \< DATEADD(mi, -59,GETDATE())”,[linje])

Works:  
system.db.runPrepQuery(“SELECT mProdusert FROM linje1\_time WHERE t\_stamp \> DATEADD(mi, -60,GETDATE()) AND t\_stamp \< DATEADD(mi, -59,GETDATE())”,[])

Is there any tricks to get around this?

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [December 17, 2020, 2:29pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepquery-with-from-as-a-argument/41610/2 "2020-12-17T14:29:18Z")

</div>

> [@SBL\_MC](#):
>
> linje = “linje1\_time”  
> system.db.runPrepQuery(“SELECT mProdusert FROM ? WHERE t\_stamp \> DATEADD(mi, -60,GETDATE()) AND t\_stamp \< DATEADD(mi, -59,GETDATE())”,[linje])

How about:

```auto
linje = "linje1_time"
query = "SELECT mProdusert FROM %s WHERE t_stamp > DATEADD(mi, -60,GETDATE()) AND t_stamp < DATEADD(mi, -59,GETDATE())" % (linje)
system.db.runPrepQuery(query,[])

```

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [December 17, 2020, 2:30pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepquery-with-from-as-a-argument/41610/3 "2020-12-17T14:30:07Z")

</div>

```auto
linje = “linje1_time”
sqlQuery = 'SELECT mProdusert FROM {} WHERE t_stamp > DATEADD(mi, -60,GETDATE()) AND t_stamp < DATEADD(mi, -59,GETDATE())'.format(linje )
system.db.runPrepQuery(sqlQuery,[])

```

If you aren’t passing in any parameters though you can just just the system.db.runQuery:

```auto
linje = “linje1_time”
sqlQuery = 'SELECT mProdusert FROM {} WHERE t_stamp > DATEADD(mi, -60,GETDATE()) AND t_stamp < DATEADD(mi, -59,GETDATE())'.format(linje)
system.db.runQuery(sqlQuery)

```

---

<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:** [December 17, 2020, 2:32pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepquery-with-from-as-a-argument/41610/4 "2020-12-17T14:32:38Z")

</div>

To answer your question, there is no tricks to get around this. SQL will not allow schema objects (tables, columns, etc.) to be sent as parameters.

As @bpreston and @bschroeder have shown, the only option is to make the query string dynamic.

Be careful not to allow user input into the this though as you will be open to SQL injection.

---

<div class="post-metadata">

**Author:** ![SBL\_MC](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sbl_mc/32/56813_2.png) [@SBL\_MC](https://forum.inductiveautomation.com/u/SBL_MC)\
**Post date:** [December 17, 2020, 2:34pm UTC](https://forum.inductiveautomation.com/t/system-db-runprepquery-with-from-as-a-argument/41610/5 "2020-12-17T14:34:53Z")

</div>

Thanks all. That was quick.  
Made the query dynamic as suggested by bpreston.
