# RunPrepQuery in a "not default" SQL server

**URL:** <https://forum.inductiveautomation.com/t/runprepquery-in-a-not-default-sql-server/19735>\
**Category:** Ignition\
**Tags:** database, scripting\
**Created:** [September 19, 2018, 3:41pm UTC](https://forum.inductiveautomation.com/t/runprepquery-in-a-not-default-sql-server/19735 "2018-09-19T15:41:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![valet81](https://avatars.discourse-cdn.com/v4/letter/v/c77e96/32.png) [@valet81](https://forum.inductiveautomation.com/u/valet81)\
**Post date:** [September 19, 2018, 3:41pm UTC](https://forum.inductiveautomation.com/t/runprepquery-in-a-not-default-sql-server/19735/1 "2018-09-19T15:41:07Z")

</div>

Hi all,

I’ve searched online and in this forum an answer to this problem,but I couldn’t find one, so I try to ask you.

What I’m trying to do is to extract results from a query, using a script (the example is simple but I would work with more complex queries).  
This usually work if i use my default SQL, since I don’t need to specify the db in the “runPrepQuery” function.  
But if I do the same thing on a secondary db, I keep having errors

this is the script

```auto
db = "NotDefault_DB"

queryString = "SELECT batch_no FROM batches WHERE cat_name = 'Process' and order_no='38'"
results= system.db.runPrepQuery(queryString,db)

```

this is the error

```auto
Traceback (most recent call last):

  File "<event:propertyChange>", line 8, in <module>

java.lang.ClassCastException: java.lang.ClassCastException: Cannot coerce value 'NotDefault_DB' into type: class [Ljava.lang.Object;

	caused by ClassCastException: Cannot coerce value 'NotDefault_DB' into type: class [Ljava.lang.Object;

Ignition v7.9.2 (b2017041315)
Java: Oracle Corporation 1.8.0_181

```

If I run the same query on a object or a tag, it works (I can select the secondary db from the dropdown menu)  
I’ve tried using also squared brackets etc, but I had just different errors.

I’m probably doing something wrong, but I can’t understand what it is 🙂

Any idea?

---

<div class="post-metadata">

**Author:** ![mrogers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mrogers/32/4110_2.png) [@mrogers](https://forum.inductiveautomation.com/u/mrogers)\
**Post date:** [September 19, 2018, 3:46pm UTC](https://forum.inductiveautomation.com/t/runprepquery-in-a-not-default-sql-server/19735/2 "2018-09-19T15:46:22Z")

</div>

The second argument in runPrepQuery is for the arguments, the third is for the database. Or you can use keyword assignments and replace just ‘db’ that you have there with ‘database = db’.

---

<div class="post-metadata">

**Author:** ![valet81](https://avatars.discourse-cdn.com/v4/letter/v/c77e96/32.png) [@valet81](https://forum.inductiveautomation.com/u/valet81)\
**Post date:** [September 19, 2018, 4:05pm UTC](https://forum.inductiveautomation.com/t/runprepquery-in-a-not-default-sql-server/19735/3 "2018-09-19T16:05:13Z")

</div>

Thank you very much for your quick reply!  
I’ve used the ‘database = db’ option and it worked well.

I remember trying earlier the function filling also arguments and it wasn’t working.  
I’ll double check tomorrow anyway!

Thank you
