# How to store tag values in DB on trigger tag value change and row wise inserting tag values

**URL:** <https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [March 25, 2024, 8:53am UTC](https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819 "2024-03-25T08:53:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kavya\_shree](https://avatars.discourse-cdn.com/v4/letter/k/b4bc9f/32.png) [@kavya\_shree](https://forum.inductiveautomation.com/u/kavya_shree)\
**Post date:** [March 25, 2024, 8:53am UTC](https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819/1 "2024-03-25T08:53:58Z")

</div>

Hi Team,

I want to store the tag values which are there in the instances into DB only when trigger tag value changes.

I tried using transactions but all tag will be stored into DB as column names.  
I want to store all these tags as in rows.

I tried using query tag using insert query. I am stuck in between.

please guide the steps how can I achieve this.  
I am trying to store in this format.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/1/21e06a2f82fcff2efd48ee752cb7d49ef3dc38fc.png)

Thanks

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 25, 2024, 9:41am UTC](https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819/2 "2024-03-25T09:41:18Z")

</div>

```auto
INSERT INTO TableName (MachineID, TagName, changedValue)
VALUES
  ('M100', 'tagA', ''),
  ('', 'tagB', ''),
  ('', 'tagC', ''),
  ('', 'tagD', '')

```

> **[SQL INSERT INTO Statement](https://www.w3schools.com/sql/sql_insert.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

---

<div class="post-metadata">

**Author:** ![kavya\_shree](https://avatars.discourse-cdn.com/v4/letter/k/b4bc9f/32.png) [@kavya\_shree](https://forum.inductiveautomation.com/u/kavya_shree)\
**Post date:** [March 25, 2024, 10:30am UTC](https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819/3 "2024-03-25T10:30:28Z")

</div>

thanks,

But if we have many tags, in insert query we need to write all tags. so if we want many machines instances, its more of writing .

I am trying with browsing tags and inserting.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 25, 2024, 10:36am UTC](https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819/4 "2024-03-25T10:36:29Z")

</div>

Use a script to loop through all the tags and add them to the `VALUES` list.

> **[system.tag.readBlocking | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-tag/system-tag-readBlocking)**
>
> Reads the value of the tags at the given paths.

Note how to use **one readBlocking call** to read all the tags at once.

> **[system.db.runPrepUpdate | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-db/system-db-runPrepUpdate)**
>
> Runs a prepared statement against the database, returning the number of rows that were affected.

---

<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:** [March 25, 2024, 11:39am UTC](https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819/5 "2024-03-25T11:39:09Z")

</div>

If it is always the same tags, use a **Block** transaction group.

---

<div class="post-metadata">

**Author:** ![kavya\_shree](https://avatars.discourse-cdn.com/v4/letter/k/b4bc9f/32.png) [@kavya\_shree](https://forum.inductiveautomation.com/u/kavya_shree)\
**Post date:** [March 25, 2024, 11:42am UTC](https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819/6 "2024-03-25T11:42:00Z")

</div>

I am able to insert tag name but same code I applied to insert tag value. I am getting error. I am trying in script console.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/c/1cc623d00a7ba78cfcb68b3a618aee73571b2373.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/9/a929c020d80dca6ac05884f0cb6eb4926ce61a9d.png)

```auto
tags = system.tag.browse("[default]")
for t in tags:
	
	value + = system.tag.readBlocking([tags])[0].value
	
	query = 'INSERT INTO Table (CHANGED_VALUE) VALUES (?)'
	system.db.runPrepUpdate(query, [value])

```

---

<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:** [March 25, 2024, 11:46am UTC](https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819/7 "2024-03-25T11:46:01Z")

</div>

> [@kavya\_shree](#):
>
> I want to store all these tags as in rows.

You probably should also explain this. It is generally a very bad idea to **not** store timestamp/condition-related values in the same row.

---

<div class="post-metadata">

**Author:** ![kavya\_shree](https://avatars.discourse-cdn.com/v4/letter/k/b4bc9f/32.png) [@kavya\_shree](https://forum.inductiveautomation.com/u/kavya_shree)\
**Post date:** [March 25, 2024, 12:14pm UTC](https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819/8 "2024-03-25T12:14:25Z")

</div>

This script is only for testing purpose.  
In my table, machineId, ordernumber, tagname, changedtime, changedvalue columns are there.  
on trigger tag, in ValueChange, I can write this script know.  
so that whenever trigger value will change I am trying to insert these changed values.

is it correct. any other method pls guide.

Thank you

---

<div class="post-metadata">

**Author:** ![kavya\_shree](https://avatars.discourse-cdn.com/v4/letter/k/b4bc9f/32.png) [@kavya\_shree](https://forum.inductiveautomation.com/u/kavya_shree)\
**Post date:** [March 26, 2024, 9:31am UTC](https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819/9 "2024-03-26T09:31:13Z")

</div>

Hi,  
I am able to insert into Db in script console.  
same script I am trying in triggertag, On value change, not able to insert. please guide.

I tried using calling function from project library also.

```auto
tags = system.tag.browse("[default]")
for t in tags:
	machineid = 'RR'
	sapordernumber = 2344
	partnum = 5555
	date = '2024-03-26 00:11:56.440'
	tagname = t['name']
	tagvalue = t['value'].value
	query = 'INSERT INTO Table (MACHINE_ID,SAP_OPRDER_NUMBER,PART_NUM,UPLOAD_DATE,PARAMETER_DESCRIPTION,CHANGEDVALUE) VALUES (?,?,?,?,?,?)'
	system.db.runPrepUpdate(query, [machineid,sapordernumber,partnum,date,tagname,tagvalue])

```

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 26, 2024, 10:04am UTC](https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819/10 "2024-03-26T10:04:15Z")

</div>

The manual has some clues that are easy to miss: [system.db.runPrepUpdate | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-db/system-db-runPrepUpdate).

There are two variants depending on whether you are running in the gateway or in a Vision client / Perspective session. You're on the gateway.

![runPrepQuery syntax](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/4/744e0af9e5c5656d261181494f78a7bb9b3aaacd.png)  
and at the bottom of that section,  
 ![Scope gateway](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/8/b81d4fd2a98a2cdcddc3d16fdbcae87f0ccd74dc.png)

The database parameter is not optional. (Designer's script console gets the database name from your project properties. The gateway hasn't a clue which database to use unless you tell it.)

---

<div class="post-metadata">

**Author:** ![kavya\_shree](https://avatars.discourse-cdn.com/v4/letter/k/b4bc9f/32.png) [@kavya\_shree](https://forum.inductiveautomation.com/u/kavya_shree)\
**Post date:** [March 27, 2024, 4:15am UTC](https://forum.inductiveautomation.com/t/how-to-store-tag-values-in-db-on-trigger-tag-value-change-and-row-wise-inserting-tag-values/87819/11 "2024-03-27T04:15:16Z")

</div>

Yes I missed DB connection name in parameter.  
I got the solution.

Thank you
