# Unable to insert with a simple select statement

**URL:** <https://forum.inductiveautomation.com/t/unable-to-insert-with-a-simple-select-statement/17145>\
**Category:** Ignition\
**Created:** [February 26, 2018, 9:10pm UTC](https://forum.inductiveautomation.com/t/unable-to-insert-with-a-simple-select-statement/17145 "2018-02-26T21:10:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrick.garceau](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@patrick.garceau](https://forum.inductiveautomation.com/u/patrick.garceau)\
**Post date:** [February 26, 2018, 9:10pm UTC](https://forum.inductiveautomation.com/t/unable-to-insert-with-a-simple-select-statement/17145/1 "2018-02-26T21:10:50Z")

</div>

Code is databaseConnection = “Ignition”

# Insert missing tags from database

sqlString = “”"  
insert into aws\_log(tagpath,lasttime)  
select distinct tagpath , (select MIN(start\_time) from sqlth\_partitions)  
from dbo.sqlth\_te t  
where not exists(select \* from aws\_log where tagpath=t.tagpath)  
“”"

system.db.runPrepUpdate(sqlString)  
print sqlString

Error is : java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(  
insert into aws\_log(tagpath,lasttime)  
select distinct tagpath , (select MIN(start\_time) from sqlth\_partitions)  
from dbo.sqlth\_te t  
where not exists(select \* from aws\_log where tagpath=t.tagpath)  
, [null], , , false, false)

Any idea what is wrong? Should I just do row by row with a insert with values?  
New to this software, trying to find if a project is do-able, crashed at the first statement, oh well.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 26, 2018, 11:38pm UTC](https://forum.inductiveautomation.com/t/unable-to-insert-with-a-simple-select-statement/17145/2 "2018-02-26T23:38:32Z")

</div>

Take a look at the full error details, at the “Caused by” sections, for information on what your database doesn’t like. Be sure your SQL statement actually works in a SQL management studio before you expect Ignition to run it.  
Aside from that, direct access to sqlt\_\* and sqlth\_\* tables is not for the faint of heart – certainly not recommended, and certainly not for beginners. Consider taking the (free) online training at [Inductive University](https://www.inductiveuniversity.com/library/) to get started.

---

<div class="post-metadata">

**Author:** ![patrick.garceau](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@patrick.garceau](https://forum.inductiveautomation.com/u/patrick.garceau)\
**Post date:** [February 27, 2018, 2:32pm UTC](https://forum.inductiveautomation.com/t/unable-to-insert-with-a-simple-select-statement/17145/3 "2018-02-27T14:32:16Z")

</div>

Hi, first, this statement has been tested in SQL Server, as a DBA, I know exactly how to write complex code. The SQL server is not the issue as this code was fully tested and now I need to implement this into Ignition.

So I changed the query to : insert into aws\_log(tagpath,lasttime) select distinct tagpath , 1517461200000 from dbo.sqlth\_te t where not exists (select \* from aws\_log where tagpath=t.tagpath)

x = system.db.runPrepUpdate(sqlString,[],“Ignition”,"",1,1)

The result is :

Caused by: java.lang.Exception: Error executing system.db.runPrepUpdate(insert into aws\_log(tagpath,lasttime) select distinct tagpath , 1517461200000 from dbo.sqlth\_te t where not exists (select \* from aws\_log where tagpath=t.tagpath), [], Ignition, , true, true)  
Caused by: com.inductiveautomation.ignition.client.gateway\_interface.GatewayException: Cannot run this function when in read-only mode.

is it possible that I cannot run an update from ignition?  
What does this mean, read-only mode?

---

<div class="post-metadata">

**Author:** ![Nathan\_ICS](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nathan_ics/32/4047_2.png) [@Nathan\_ICS](https://forum.inductiveautomation.com/u/Nathan_ICS)\
**Post date:** [February 27, 2018, 2:44pm UTC](https://forum.inductiveautomation.com/t/unable-to-insert-with-a-simple-select-statement/17145/4 "2018-02-27T14:44:06Z")

</div>

You need to change your designer to read/write mode. Its in the Project menu.

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [February 27, 2018, 2:46pm UTC](https://forum.inductiveautomation.com/t/unable-to-insert-with-a-simple-select-statement/17145/5 "2018-02-27T14:46:16Z")

</div>

Read-only mode means the designer won’t write to the PLC or the Database. This is to protect you when opening the designer on a live project, as it could fire triggers of any kind.

The solution is just to change the mode with one of these buttons: (the modes are “offline”, “read-only” and “read-write”)

![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/2/2a8c2300c670d8ad04d6d97c4c2817eba440445b.png)

---

<div class="post-metadata">

**Author:** ![patrick.garceau](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@patrick.garceau](https://forum.inductiveautomation.com/u/patrick.garceau)\
**Post date:** [February 27, 2018, 2:57pm UTC](https://forum.inductiveautomation.com/t/unable-to-insert-with-a-simple-select-statement/17145/6 "2018-02-27T14:57:33Z")

</div>

Thanks

This is solved.

Still does not execute complex queries but I was able to do this using For-Loop, etc…

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [February 27, 2018, 3:06pm UTC](https://forum.inductiveautomation.com/t/unable-to-insert-with-a-simple-select-statement/17145/7 "2018-02-27T15:06:04Z")

</div>

The queries are just passed on to the JDBC driver bundled with Ignition (though you can replace it with your custom JDBC driver).

Any query that can be executed by the driver can be executed on Ignition.

---

<div class="post-metadata">

**Author:** ![patrick.garceau](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@patrick.garceau](https://forum.inductiveautomation.com/u/patrick.garceau)\
**Post date:** [February 27, 2018, 3:33pm UTC](https://forum.inductiveautomation.com/t/unable-to-insert-with-a-simple-select-statement/17145/8 "2018-02-27T15:33:00Z")

</div>

Unfortunately, that statement doesn’t tell me anything.

If a statement ran into SQL server is running and I pass it to ignition, it should run. This seems not valid. The JDBC driver is interpreting a statement instead of passing it to the database and letting the database run it.

Not going to complain about this, just need to find a way to make this happen. Java = very longer coding.

Thanks,

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [February 27, 2018, 4:00pm UTC](https://forum.inductiveautomation.com/t/unable-to-insert-with-a-simple-select-statement/17145/9 "2018-02-27T16:00:31Z")

</div>

I just tried that query here (after creating a correct aws\_log table), and this is the query I saw appearing in the SQL profiler.

So the JDBC driver does surround it a bit with some code to return keys or row counts, but the query itself succeeded.

![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/3667181db4ec66169d200dcff65741f754776fc9.png)

I have no idea where your query goes wrong.
