# Insert Query with Dynamic Table Name,Colums and parameters

**URL:** <https://forum.inductiveautomation.com/t/insert-query-with-dynamic-table-name-colums-and-parameters/36653>\
**Category:** Ignition\
**Created:** [July 13, 2020, 5:55pm UTC](https://forum.inductiveautomation.com/t/insert-query-with-dynamic-table-name-colums-and-parameters/36653 "2020-07-13T17:55:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anita.Shinde](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Anita.Shinde](https://forum.inductiveautomation.com/u/Anita.Shinde)\
**Post date:** [July 13, 2020, 5:55pm UTC](https://forum.inductiveautomation.com/t/insert-query-with-dynamic-table-name-colums-and-parameters/36653/1 "2020-07-13T17:55:44Z")

</div>

Hello,  
I am trying to write a dynamic SQL Insert Query in that all values are dynamic like This:  
There will be any number of columns that is not fixed.

allCols=‘Company,Email,First\_Name’  
ColValues=‘MuleSoft,maxthemule@mulesoft.com,Max’  
Table=‘Mapping\_Info’  
DB=‘Ignition\_Integration’  
insertQuery=‘insert into ‘+str(Table)+’ (’+str(allCols)+’) values (?,?,?)’   
print ‘insertQuery’,insertQuery  
value=system.db.runPrepUpdate(insertQuery,[ColValues],str(DB))

But it is throwing exception.Is it possible to do like this?  
I have referred the solution given in forum but i didn’t get it.  
Can anyone Help me out this.  
Thanks.

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [July 13, 2020, 6:04pm UTC](https://forum.inductiveautomation.com/t/insert-query-with-dynamic-table-name-colums-and-parameters/36653/2 "2020-07-13T18:04:23Z")

</div>

Try modifying your script to something like this:

```auto
colValues = ['MuleSoft','maxthemule@mulesoft.com','Max']
allCols = ['Company', 'Email', 'First_Name']
cols = ''
for col in allCols:
	cols += col + '=? '
table = 'Mapping_Info'
query = "update %s set %s" %(table, cols)
database = 'Ignition_Integration'
system.db.runPrepUpdate(query, colValues , database=database)

```

---

<div class="post-metadata">

**Author:** ![Anita.Shinde](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Anita.Shinde](https://forum.inductiveautomation.com/u/Anita.Shinde)\
**Post date:** [July 13, 2020, 6:46pm UTC](https://forum.inductiveautomation.com/t/insert-query-with-dynamic-table-name-colums-and-parameters/36653/3 "2020-07-13T18:46:34Z")

</div>

Thanks for the reply.  
I have tried ,There is some syntax error in this but It will work for update query.  
I am facing issue with insert query, can you please explain for insert.

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [July 13, 2020, 6:53pm UTC](https://forum.inductiveautomation.com/t/insert-query-with-dynamic-table-name-colums-and-parameters/36653/4 "2020-07-13T18:53:36Z")

</div>

@Anita.Shinde sorry about that.

Try this for an insert:

```auto
colValues = ['MuleSoft','maxthemule@mulesoft.com','Max']
cols = 'Company, Email, First_Name'
args = '?,' * len(colValues)
args = args[:-1]
table = 'Mapping_Info'
query = "insert into %s (%s) VALUES (%s)" %(table, cols, args)
database = 'Ignition_Integration'
system.db.runPrepUpdate(query, colValues , database=database)

```

---

<div class="post-metadata">

**Author:** ![Anita.Shinde](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Anita.Shinde](https://forum.inductiveautomation.com/u/Anita.Shinde)\
**Post date:** [July 13, 2020, 7:40pm UTC](https://forum.inductiveautomation.com/t/insert-query-with-dynamic-table-name-colums-and-parameters/36653/5 "2020-07-13T19:40:22Z")

</div>

Thank You. It’s working.
