# Syntax for database name MSSQL

**URL:** <https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735>\
**Category:** Ignition\
**Created:** [June 29, 2012, 2:16pm UTC](https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735 "2012-06-29T14:16:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jerry\_H](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@Jerry\_H](https://forum.inductiveautomation.com/u/Jerry_H)\
**Post date:** [June 29, 2012, 2:16pm UTC](https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735/1 "2012-06-29T14:16:38Z")

</div>

Need syntax to do an Insert query to a Table that is not on the Default database:

Insert into [Ignition\_PLU].[dbo].[Scrap\_Scales] (Scrap\_Lbs, etc)

Query works if I use the default database, but I need to use a different Database than the default.

---

<div class="post-metadata">

**Author:** ![James.Lorenz](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/james.lorenz/32/3290_2.png) [@James.Lorenz](https://forum.inductiveautomation.com/u/James.Lorenz)\
**Post date:** [June 29, 2012, 3:21pm UTC](https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735/2 "2012-06-29T15:21:40Z")

</div>

If you are using a SQL query for a property binding, SQL tag, or something similar, you will be provided a dropdown box with the list of database connections to choose. If you’re scripting, each of the scripting functions can take the name of the database connection as a parameter. To do any operations on a database, each database connection must be properly set up in the Ignition Gateway.

For information about scripting and database configurations take a look at our online manual:

[inductiveautomation.com/support/ … /ignition/](http://inductiveautomation.com/support/usermanuals/ignition/)

---

<div class="post-metadata">

**Author:** ![Bassafish](https://avatars.discourse-cdn.com/v4/letter/b/ee7513/32.png) [@Bassafish](https://forum.inductiveautomation.com/u/Bassafish)\
**Post date:** [June 29, 2012, 3:27pm UTC](https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735/3 "2012-06-29T15:27:34Z")

</div>

I have a button and I want to insert Data to 2 tables on 2 different servers. The default database works good. but the non-default table fails.

---

<div class="post-metadata">

**Author:** ![James.Lorenz](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/james.lorenz/32/3290_2.png) [@James.Lorenz](https://forum.inductiveautomation.com/u/James.Lorenz)\
**Post date:** [June 29, 2012, 3:42pm UTC](https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735/4 "2012-06-29T15:42:00Z")

</div>

Bassafish, please post your topic in a new thread. Even though your problem is similar, it can be confusing to the original poster if there are other conversations going on in a single thread. It also keeps things organized on our end, having one problem per thread is the easiest way for our tracking system to handle the forum. Also, it also makes “solving” a thread more difficult if there are multiple users with different questions. Thanks.

---

<div class="post-metadata">

**Author:** ![Jerry\_H](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@Jerry\_H](https://forum.inductiveautomation.com/u/Jerry_H)\
**Post date:** [June 29, 2012, 3:48pm UTC](https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735/5 "2012-06-29T15:48:45Z")

</div>

yes I am using a script. I want to insert data into 2 tables (Same name) but they are on 2 different servers. They both work if they are selected as the default database connection.

Is it possible to change the database connection in the script?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [June 29, 2012, 3:51pm UTC](https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735/6 "2012-06-29T15:51:11Z")

</div>

[quote=“Jerry\_H”]yes I am using a script. I want to insert data into 2 tables (Same name) but they are on 2 different servers. They both work if they are selected as the default database connection.

Is it possible to change the database connection in the script?[/quote]

Yes, the system.db scripting functions all take an optional database name parameter. See [the user manual](http://www.inductiveautomation.com/support/usermanuals/ignition/system_db_runupdatequery.htm) for more info.

---

<div class="post-metadata">

**Author:** ![Jerry\_H](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@Jerry\_H](https://forum.inductiveautomation.com/u/Jerry_H)\
**Post date:** [June 29, 2012, 4:02pm UTC](https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735/7 "2012-06-29T16:02:38Z")

</div>

I see that in the manual, but I what is the Syntax for MSsql. The manual does not have any examples on how to switch servers and database connections.

Examples only show the table name.

userText = event.source.parent.getComponent(“TextArea”).text  
userName = system.security.getUsername()  
system.db.runPrepUpdate(“INSERT INTO Comments (Name, UserComment) VALUES (?,?)”, [userName, userText])

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [June 29, 2012, 4:11pm UTC](https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735/8 "2012-06-29T16:11:41Z")

</div>

There’s no special syntax, it’s just another parameter in the scripting call.

```auto
system.db.runUpdateQuery("INSERT INTO RecipeSteps (StepNum, Gallons) VALUES (%d, %f)" % (nextStepNum, gallons))
```

vs.

```auto
system.db.runUpdateQuery("INSERT INTO RecipeSteps (StepNum, Gallons) VALUES (%d, %f)" % (nextStepNum, gallons), "the_name_of_my_db_con")
```

---

<div class="post-metadata">

**Author:** ![Jerry\_H](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@Jerry\_H](https://forum.inductiveautomation.com/u/Jerry_H)\
**Post date:** [June 29, 2012, 6:16pm UTC](https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735/9 "2012-06-29T18:16:54Z")

</div>

Kevin  
Below is the actual script I am trying to run. I added the db connection at the end as shown in your example above, it failed. The script works if I run it on the default database connection. iI need to run it on another server database connection which is “Ignition\_PLU”.

fpmi.db.runPrepStmt(“INSERT INTO Scrap\_Scales (Mac\_Code,Job,DtCategoryCode,DTCode,ScrapLbs,Source\_Ref,REP\_TIME) VALUES (?,?,?,?,?,?,?)”,  
[Mac\_Code,Job,DtCategoryCode,DTCode,ScrapLbs,Source\_Ref,REP\_TIME],“Ignition\_Plu”)

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [June 29, 2012, 6:43pm UTC](https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735/10 "2012-06-29T18:43:47Z")

</div>

Are you using FactoryPMI or Ignition? What’s the error you are getting?

Edit: I see you’re using the deprecated runPrepStmt() call, which needs a 3rd Object[] args parameter after the datasource…

---

<div class="post-metadata">

**Author:** ![Jerry\_H](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@Jerry\_H](https://forum.inductiveautomation.com/u/Jerry_H)\
**Post date:** [June 29, 2012, 6:51pm UTC](https://forum.inductiveautomation.com/t/syntax-for-database-name-mssql/4735/11 "2012-06-29T18:51:11Z")

</div>

Using Ignition, but running some converted scripts from PMI.

Ok ! Got it, Works good now. Both Databases and servers are updating properly.

Thanks for your help !
