# Two databases in one Event Handler

**URL:** <https://forum.inductiveautomation.com/t/two-databases-in-one-event-handler/8271>\
**Category:** Ignition\
**Created:** [April 7, 2014, 8:12pm UTC](https://forum.inductiveautomation.com/t/two-databases-in-one-event-handler/8271 "2014-04-07T20:12:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tim/32/3361_2.png) [@Tim](https://forum.inductiveautomation.com/u/Tim)\
**Post date:** [April 7, 2014, 8:12pm UTC](https://forum.inductiveautomation.com/t/two-databases-in-one-event-handler/8271/1 "2014-04-07T20:12:39Z")

</div>

I would like to insert data from one database table into another database table when the user clicks a button. Both databases reside on the same SQL 2008 server. How can the database be specified in the script?

` sql = "INSERT INTO database1.table (a,b,c) SELECT (a,b,c) WHERE x FROM database2.table"
system.db.runPrepUpdate(sql) `

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [April 7, 2014, 8:16pm UTC](https://forum.inductiveautomation.com/t/two-databases-in-one-event-handler/8271/2 "2014-04-07T20:16:48Z")

</div>

How about:

```auto
sql = "INSERT INTO database1.dbo.table (a,b,c) SELECT (a,b,c) FROM database2.dbo.table WHERE x "
system.db.runPrepUpdate(sql,[]) 
```

---

<div class="post-metadata">

**Author:** ![Tim](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tim/32/3361_2.png) [@Tim](https://forum.inductiveautomation.com/u/Tim)\
**Post date:** [April 7, 2014, 8:39pm UTC](https://forum.inductiveautomation.com/t/two-databases-in-one-event-handler/8271/3 "2014-04-07T20:39:30Z")

</div>

That seems to work. If I run the following in the database query browser I get my insert. If I run with my code on the button I get an error.

```auto
INSERT INTO AX_CarrierTrack.dbo.tblSequence (ORDER_NUMBER) SELECT (ORDER_NUMBER) FROM AX_POI.dbo.POI_HEADER WHERE ORDER_NUMBER = 014002473859
```

[code]Traceback (most recent call last):

File “event:actionPerformed”, line 7, in

java.lang.Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO AX\_CarrierTrack.dbo.tblSequence (ORDER\_NUMBER) SELECT (ORDER\_NUMBER) FROM AX\_POI.dbo.POI\_HEADER WHERE ORDER\_NUMBER = 014002473859, [null], , , false, false)

```
caused by Exception: Error executing system.db.runPrepUpdate(INSERT INTO AX_CarrierTrack.dbo.tblSequence (ORDER_NUMBER) SELECT (ORDER_NUMBER) FROM AX_POI.dbo.POI_HEADER WHERE ORDER_NUMBER = 014002473859, [null], , , false, false)
caused by GatewayException: Unexpected error for "INSERT INTO AX_CarrierTrack.dbo.tblSequence (ORDER_NUMBER) SELECT (ORDER_NUMBER) FROM AX_POI.dbo.POI_HEADER WHERE ORDER_NUMBER = 014002473859"
caused by NullPointerException

```

Ignition v7.6.6 (b2014040112)  
Java: Sun Microsystems Inc. 1.6.0\_31[/code]

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [April 7, 2014, 8:46pm UTC](https://forum.inductiveautomation.com/t/two-databases-in-one-event-handler/8271/4 "2014-04-07T20:46:53Z")

</div>

You may be calling the system.db.runPrepUpdate function incorrectly.

Try this:

```auto
query = """INSERT INTO AX_CarrierTrack.dbo.tblSequence (ORDER_NUMBER) 
SELECT ORDER_NUMBER FROM AX_POI.dbo.POI_HEADER WHERE ORDER_NUMBER = ?"""
system.db.runPrepUpdate(query,"014002473859")
```

---

<div class="post-metadata">

**Author:** ![Tim](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tim/32/3361_2.png) [@Tim](https://forum.inductiveautomation.com/u/Tim)\
**Post date:** [April 9, 2014, 2:38pm UTC](https://forum.inductiveautomation.com/t/two-databases-in-one-event-handler/8271/5 "2014-04-09T14:38:33Z")

</div>

Thanks Nick, that’s it.
