IMDTouchKnowledge BaseShop main store

Explainer

Why a WinCC Multi-Tag Query Adds Rows Not Value Columns

HMI and touchscreens Published 5 min read By IMDTouch engineering team

Short answer

The WinCC OLE DB Provider always returns the same five fields per record: VarID, DateTime, RealValue, Quality and Flags. Adding tags to the query lengthens the result, it never widens it, so you must separate rows by VarID, match them by timestamp and assemble the four- or six-column line string yourself.

On this page
  1. Why does the recordset grow in rows instead of columns?
  2. Which fields does a WinCC OLE DB archive record return?
  3. How do you tell which tag a row belongs to?
  4. When is a four-column layout valid?
  5. How do you assemble the line before calling WriteLine?
  6. What should you check in the finished CSV?
  7. What is the decision for each layout?
  8. Questions asked

A WinCC recordset from the OLE DB Provider carries a fixed record layout, so a query listing three archive tags returns about three times as many rows, each holding one RealValue, not a wider table with three value fields. Extending the existing WriteLine expression with more field names therefore cannot produce a four- or six-column CSV; the columns have to be built in the script.

Why does the recordset grow in rows instead of columns? #

The provider returns one record per archived sample per tag, so each tag you add to the query multiplies the row count instead of adding a RealValue field to every row. This is documented behavior, not a scripting error: archive data is stored compressed, and the provider decompresses it into the same fixed shape for every tag. A query against one tag over one hour with, for example, 600 stored samples returns 600 records; the same query naming three tags returns roughly 1,800 records with 600 per tag.

Which fields does a WinCC OLE DB archive record return? #

Every record from a process value archive query has exactly five fields, in this order: Fields(0) is the VarID, Fields(1) is DateTime, Fields(2) is RealValue, Fields(3) is Quality and Fields(4) is Flags. Your working one-tag script already uses two of them, TimeStamp and RealValue, and the other three exist whether or not you read them. There is no sixth field to ask for, so a four-column output needs one timestamp written next to three RealValues that the script has matched, and a six-column output needs three timestamp/value pairs assembled into one string.

The one place where the single-record view breaks down in practice is field availability: if a query suddenly reports RealValue missing after a runtime update, that is a separate provider problem, and the diagnosis in the article on the WinCC OLE DB Provider RealValue field disappearing in WinCC 7.4 SP1 Update 12 is where to start.

How do you tell which tag a row belongs to? #

Use the VarID, the value in Fields(0), to assign each record to its tag. The provider identifies tags in the query through the archive naming, but it returns only the numeric VarID in the record, and it does not publish which number belongs to which tag. Determine the mapping from your own data before pivoting: run the multi-tag query over a short range, group the rows by VarID, and confirm which group behaves like which tag, for example the group that tracks the oven temperature is the oven tag. Store that mapping in a small array or lookup so the script can place each RealValue in the right column.

A three-tag query like the one from the original script, Tag:R,(ProductionTags\Temperature1;ProductionTags\Temperature2;ProductionTags\Temperature3),'StartArchive','StopArchive', then yields three interleaved record streams inside one recordset, each stream recognizable only by its VarID.

When is a four-column layout valid? #

A four-column row, timestamp followed by three values, is only valid after the script has matched records from all three tags by timestamp, because independently archived tags are not guaranteed to have a sample at the same instant. Each tag is sampled and compressed on its own, so a 12:00:00 record for one tag can sit next to a 12:00:00.125 record for another.

Worked example: suppose the archive holds these three records in the matched interval.

VarID DateTime RealValue
11 2024-05-06 12:00:00.000 63.5
12 2024-05-06 12:00:00.125 41.2
13 2024-05-06 12:00:00.000 7.9

If the report tolerates values within one second sharing a row, the script truncates each timestamp to whole seconds and matches on the result: all three records then belong to the 12:00:00 row, and the script writes 12:00:00;63.5;41.2;7.9. If the report requires exact timestamps, no match exists here and the four-column layout cannot be used honestly; write the six-column form instead, 12:00:00.000;63.5;12:00:00.125;41.2;12:00:00.000;7.9. The decision rule is simple: truncate to the reporting interval only when the reporting requirement accepts values inside that interval on one line.

The same row-per-tag structure appears in other historians too; the article on Ignition's AsStored wide history mode adding extra rows describes the matching wide-versus-tall decision from the other side, and it is worth reading before you settle the CSV layout.

How do you assemble the line before calling WriteLine? #

Buffer the timestamp and RealValue pairs first, match them, and only then build one string per row and hand that string to WriteLine. Writing directly inside the record loop is what produced the original two-column file, and no reformulation of the loop's WriteLine argument will change that.

  1. Run the multi-tag query once, or run one single-tag query per tag in sequence; both work, because the pivot happens in the script either way.
  2. Separate the records by VarID into three collections, each holding timestamp/value pairs.
  3. Match entries across the collections using the timestamp rule you chose, truncating only if the report allows it, and mark unmatched entries instead of shifting later values into the wrong columns.
  4. Build the delimited string per completed row and write it.
' Four-column row after timestamp matching, VBA-style pseudocode
' ts is the TextStream opened as in the original script
rowText = matchedTime & ";" & value1 & ";" & value2 & ";" & value3
ts.WriteLine rowText

' Six-column row when each value keeps its own timestamp
rowText = time1 & ";" & value1 & ";" & time2 & ";" & value2 & ";" & time3 & ";" & value3
ts.WriteLine rowText

The variable names here are illustrative; the objects your script already creates, the TextStream and the ADODB recordset, stay exactly as they are. Only the loop body changes from writing each record immediately to accumulating, matching and then writing assembled lines.

What should you check in the finished CSV? #

Open the file and verify the column count and several timestamp/value pairs against the recordset before anyone reports from it. Three checks catch every failure mode this layout produces: every source record landed in the column of its own tag, the timestamp rule did not merge two distinct samples of one tag into one cell, and every row where one tag had no match shows an empty cell rather than a value borrowed from a neighbor. If a check fails, the mapping table or the matching interval, not the query, is the thing to fix.

What is the decision for each layout? #

Choose the six-column layout when each tag keeps its own archive cycle or the report demands exact sample times; it needs no matching and cannot misalign. Choose the four-column layout only when the report accepts all samples inside one interval on a shared timestamp: truncate, match on the truncated time, and write timestamp;value1;value2;value3. In both cases the multi-tag query is fine as written; the width of the CSV is decided entirely by the script that pivots the fixed five-field records into columns.

Questions asked

How do I identify which archive tag a returned record belongs to?

Read the first field of each record, which holds the VarID. The provider does not publish a fixed assignment, so request a short range, note which VarID appears with which known tag behavior or cross-check the archive configuration, then build a small lookup table that maps each VarID to its tag name before pivoting records into CSV columns.

Why do the three values rarely land on exactly the same timestamp?

Each archive tag is sampled and compressed independently, so the record times for the three tags in one cycle can differ by milliseconds. If your report requires one shared timestamp, round all times to the reporting interval before matching, and accept that the row shows the nearest stored sample for each tag within that interval.

Can I query the archive database directly with SQL instead?

Yes. The WinCC OLE DB Provider can be registered in SQL Server as a linked server, whether the runtime database sits locally or on a remote machine, which lets you run the same Tag:R queries from SQL tooling. The record layout and the row-per-tag behavior stay the same, so the pivot logic is still yours to write.

What is the Quality field for in an archive record?

It reports the archived data quality of that sample, such as whether the value came from good processing or was substituted after a communication failure. Check it when you pivot: if a tag has no good-quality record inside the matching interval, leave its CSV cell empty instead of silently writing the nearest stored value.

Keep reading

  • Explainer

    Why WinCC Unified scripts export tag data as CSV, not Excel

    A WinCC Unified runtime script cannot write an .xlsx file: its JavaScript environment exposes tags, screen items, alarms and system functions, with no file-system or zip library, and an Excel workbook is a zipped XML package. Log the tags, select a start and end time on a screen, and export that range as CSV from the trend control's export or an external log reader, converting UTC to local time in one place.

    HMI and touchscreens

  • Troubleshooting

    Why WinCC Runtime says no connection to data server

    A running CCAlgRtServer.exe proves only that the process started, not that Runtime reached the project's data server. When the Windows computer name differs from the configured OS computer identity, recreate the OPC UA configuration, restart and compile the OS entirely; otherwise delete the generated dcf file on the engineering station, compile the OS server with server packages, then load and restart. Alarms returning in the Runtime alarm area is the pass.

    HMI and touchscreens

  • Troubleshooting

    Why one WinCC redundant server freezes trends on insufficient memory

    Server 2 freezes because its SQL Server max server memory is left at the factory default of 2147483647 MB, which caps nothing, so SQL growth exhausts Windows commit and the event log reports insufficient system memory. Set both redundant servers to the same measured budget — Server 1 already proves the project runs at its 2048 MB setting — match the archive segment configuration, and verify commit headroom stays open through a full freeze.

    HMI and touchscreens

Why WinCC Runtime Advanced simulation adds a DE keyboard layout