RunPrepQuery in a "not default" SQL server

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

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

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 :slight_smile:

Any idea?

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’.

1 Like

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

1 Like