runQuery on MSSql

In a WebDev Python script I have tried

alarmEvents = system.db.runQuery(“select * from alarm_events”)

and

alarmEvents = system.db.runQuery(“select * from dbo.alarm_events”)

but i get

at java.lang.reflect.Method.invoke(Unknown Source) java.lang.Exception: java.lang.Exception: Error executing system.db.runQuery(select * from dbo.alarm_events, , )

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File “”, line 30, in doGet

Can you post the full error stacktrace?

It’s almost certainly what @KathyApplebaum mentioned; because WebDev resources run in the gateway scope, there is no ‘default’ datasource to use - so the script will error out. Pass in the DB name as the second argument to runQuery, eg:

system.db.runQuery("SELECT * FROM SOME_TABLE", "DB")

And your script will likely work correctly.

Added “DB” e.g.

alarmEvents = system.db.runQuery("select * from dbo.alarm_events", "DB")

But still got error e.g.

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File “”, line 30, in doGet
at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.error(AbstractDBUtilities.java:364)
at com.inductiveautomation.ignition.common.script.builtin.AbstractDBUtilities.runQuery(AbstractDBUtilities.java:335)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source) java.lang.Exception: java.lang.Exception: Error executing system.db.runQuery(select * from dbo.alarm_events, DB, )

Any suggestions appreciated.

Is that the full stacktrace? If not, then please post the full stack trace. Sometimes the full stack trace indicates what is wrong.

With MS SQL I have had problems referencing table and field names without surrounding them in square brackets.
Have you tried “SELECT * FROM [alarm_events]”?