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

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

Thanks

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

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.

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

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

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

1 Like

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
image

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])

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

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

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.

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])

The manual has some clues that are easy to miss: system.db.runPrepUpdate | Ignition User Manual.

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
and at the bottom of that section,
Scope gateway

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.)

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

Thank you