IMD SolutionComputing LibraryShop main store

Troubleshooting

Why an OPENQUERY historian query fails as a 128-character identifier

Edge computing and IIoT Published 6 min read By IMD Solution engineering team

Short answer

The error is SQL Server's identifier limit, not a historian or driver limit. With QUOTED_IDENTIFIER on, the double-quoted second argument of OPENQUERY is parsed as an identifier, and anything past 128 characters fails. Wrap the inner SELECT in single quotes, keep the historian values in their own quotes, and check the time filter the browser actually submitted.

On this page
  1. What the identifier error is measuring
  2. Why OPENQUERY is in the statement at all
  3. Why double quotes break the parse
  4. The corrected statements
  5. Check the statement the browser echoed
  6. Why the same text ran in Management Studio
  7. The decision for each symptom
  8. Questions asked

Your statement is fine; the quotes around it are not. The 128-character identifier error on the OPENQUERY historian query means SQL Server read the entire inner SELECT as one quoted name, because the query browser submitted the OPENQUERY argument in double quotes. Nothing reached the historian. Wrap the inner query in single quotes and the statement parses on every connection; then read the statement the browser echoes, because the same echo that exposed the quoting fault also showed the less-than character missing from the upper time bound. Both fixes are one editing pass on the statement you already have.

What the identifier error is measuring #

Read the whole error before touching the connection or the tag list. In the reported case the statement ran in SQL Server Management Studio and failed when the same text went through an Ignition query browser, and the decisive line was the parser quoting the start of the inner query back as one name:

The identifier that starts with 'SELECT DateTime, [TAG1], [TAG2] FROM WideHistory WHERE wwRetrievalMode = 'Cyclic' AND wwResolu' is too long. Maximum length is 128.

The text between the double quotes never became a retrieval request. It became one identifier, and the documented cap for identifiers is what stopped it. The failure sits inside the parse of the outer statement, before anything is handed to the linked server, so a healthy connection and correct credentials prove nothing about this error.

Match your case before editing:

  • The error quotes the start of the inner SELECT and reports a maximum-length identifier — the outer parser read the OPENQUERY argument as a name. Check the delimiters around that argument.
  • The identical statement runs in Management Studio and fails in the query browser — the two sessions read double quotes differently. Compare the text and the session settings, then fix the text.
  • The statement parses after the quote fix but returns no rows — parsing is solved, and the time filter and retrieval settings become the suspects. Check the comparison operators the browser echoed.
  • A linked-server or provider error appears after the quote fix — the request finally reached the historian layer. Look at the linked-server definition and the provider it names.

Why OPENQUERY is in the statement at all #

The historian here is Wonderware Historian, reached through a SQL Server linked server — INSQL in the failing statement. Its retrieval tables are not ordinary tables inside the SQL Server database; they are served by the historian's own OLE DB provider, which the linked server invokes. AVEVA's documentation states that WideHistory, the wide view returning the values of one or more tags at a single timestamp, is reachable only through OPENQUERY. OPENQUERY requires its second argument to be a string, which the server decodes and hands to the provider. Keep the two layers apart: SQL Server parses the OPENQUERY call, and the provider parses the decoded inner query. The identifier error is a first-layer fault.

Why double quotes break the parse #

Two documented SQL Server rules collide. SET QUOTED_IDENTIFIER decides what double quotes mean: with the session default, on, double quotes delimit identifiers; with the setting off, they delimit character strings and identifiers must follow the plain naming rules. OPENQUERY requires a string in that position. A session with the default on therefore reads the double-quoted SELECT as one identifier, and identifiers stop at the cap you just met.

Count the statement the browser echoed. Flattened to single spaces, the text between the double quotes is:

SELECT DateTime, [TAG1], [TAG2] FROM WideHistory WHERE wwRetrievalMode = 'Cyclic' AND wwResolution = 1000 AND wwQualityRule = 'Extended' AND wwVersion = 'Latest' AND DateTime >= DateAdd(hh,-1,GetDate()) AND DateTime = GetDate()

Clause by clause, each with its trailing space:

  • SELECT DateTime, [TAG1], [TAG2] — 32 characters
  • FROM WideHistory — 17
  • WHERE — 6
  • wwRetrievalMode = 'Cyclic' — 27
  • AND — 4
  • wwResolution = 1000 — 20
  • AND — 4
  • wwQualityRule = 'Extended' — 27
  • AND wwVersion = 'Latest' — 25
  • AND DateTime >= DateAdd(hh,-1,GetDate()) — 41
  • AND DateTime = GetDate() — 24

The sum is 32 + 17 + 6 + 27 + 4 + 20 + 4 + 27 + 25 + 41 + 24 = 227 characters, against the 128-character identifier limit. The parser rejects the whole text as one over-long name before the historian is involved, which is why the error quotes your SELECT back at you. Shortening the query changes only which error appears next; the argument still is not a string.

The corrected statements #

Pass the inner query in single quotes. Form one keeps the values in double quotes, the style used in the vendor's own retrieval examples inside the single-quoted OPENQUERY string:

SELECT * FROM OPENQUERY(INSQL, 'SELECT DateTime, [TAG1], [TAG2] FROM WideHistory WHERE wwRetrievalMode = "Cyclic" AND wwResolution = 1000 AND wwQualityRule = "Extended" AND wwVersion = "Latest" AND DateTime >= DateAdd(hh,-1,GetDate()) AND DateTime <= GetDate()') ORDER BY DateTime ASC;

Form two is the standard SQL escape: single quotes around the values, each doubled so it survives the outer string, and the provider receives ordinary single-quoted values after decoding:

SELECT * FROM OPENQUERY(INSQL, 'SELECT DateTime, [TAG1], [TAG2] FROM WideHistory WHERE wwRetrievalMode = ''Cyclic'' AND wwResolution = 1000 AND wwQualityRule = ''Extended'' AND wwVersion = ''Latest'' AND DateTime >= DateAdd(hh,-1,GetDate()) AND DateTime <= GetDate()') ORDER BY DateTime ASC;

Form one matches the reported fix. Pick one house style and prove it on the connection that failed. Three rules keep the statement correct as you edit it:

  • The outer argument stays single-quoted, and any single quote inside it must be doubled or it ends the string early.
  • Inner values keep their own quotes, and the numeric resolution value stays unquoted.
  • Test on the query browser session, not only in Management Studio, because the session that failed is the one that must pass.

Check the statement the browser echoed #

The same echo that exposed the quoting fault shows a second fault. The upper time bound arrived at the server as AND DateTime = GetDate(), not AND DateTime <= GetDate(): the less-than character did not survive the trip from the saved statement. Equality against the current instant returns rows only if the historian holds a sample stamped exactly on that instant, so the query can parse cleanly and still come back empty.

Work the check in this order:

  1. Compare the echoed statement with the statement you saved, operators first.
  2. Confirm the lower bound reads DateTime >= DateAdd(hh,-1,GetDate()), one hour back, and the upper bound reads DateTime <= GetDate().
  3. If the echo lost the less-than, re-enter the comparison in the statement you actually save and run it again on the same connection.
  4. Read the first and last returned timestamps against the window you asked for.

A statement that parses and still returns nothing then has ordinary historian causes, not quoting ones. The flat-line trend that a row cap produces is one of them, described in Historian trend flat line: check Query Row Limit. When SQL Server holds the rows but the client screen shows nothing, the fault lives between query and display, as in Ignition history binding shows zeros while SQL Server has values.

Why the same text ran in Management Studio #

The login is the same in both tools; the session is not. QUOTED_IDENTIFIER rides the session, not the account: with the setting off, double quotes make strings and the original statement runs; with the default on, the same text becomes an identifier and fails at the length check. That is why identical credentials do not guarantee identical parsing. Fix the text rather than the session: the single-quoted form parses identically everywhere and leaves nothing for a connection option to change.

The decision for each symptom #

  • Maximum-identifier error quoting the start of your inner SELECT — rewrite the OPENQUERY argument in single quotes, keep the inner values in double quotes or doubled single quotes, and do not shorten the query to fit.
  • Statement parses, zero rows — quoting is fixed; check the echoed operators for a lost less-than, then the time window and the retrieval settings.
  • Rows return but the window is wrong — correct the statement you saved, not only the text the tool submitted, and verify with the first and last timestamps.
  • Linked-server or provider error after the quote fix — the request reached the historian; verify the linked-server definition and the provider before editing the statement again.
  • Statement runs on every connection after the rewrite — standardize on the single-quoted form so no session setting can change the parse.

Questions asked

Why did the query work for a while and then fail with no change to the SQL?

What parses is decided by the session, not by the text alone. Double-quoted text is read as a string only in a session whose QUOTED_IDENTIFIER setting is off, so a new client build, a different driver or a changed connection option can break a statement that ran untouched. Put the fix in the text itself: the single-quoted form parses on every session, so the statement stops depending on where it runs.

Is this a limit on the OPENQUERY subquery or on the driver?

No. It is SQL Server's maximum length for an identifier, such as a table or column name, and it applies only because the double-quoted inner query was read as one identifier. A properly quoted string argument to OPENQUERY can run far longer, and no driver setting changes the identifier rule.

Can historian tables be queried without OPENQUERY at all?

WideHistory is documented as reachable only through OPENQUERY, so it always needs the single-quoted string form. Other historian views exposed through the linked server may accept a fully qualified name instead, which avoids string nesting entirely for those views. The vendor's retrieval documentation is the place to confirm which views allow it; the quote rules here apply only where OPENQUERY is used.

Can the session setting be changed instead of rewriting the quotes?

Setting QUOTED_IDENTIFIER off for the session makes double-quoted text read as a string, so the original statement can parse. It is the weaker fix: the query tool or driver may not expose the setting, every new connection can default back to on, and it removes double-quoted identifiers for the whole session. The single-quoted argument parses the same way on every connection and needs no session change.

Keep reading

  • Troubleshooting

    Fix Historian timeout error 1460 after a 23.1 upgrade

    Error 1460 after a 17.2 to 23.1 Historian upgrade is a server-side timeout: packet processing, tag retrieval and the INSQL linked-server route all miss their deadlines, so the server and every client lose tags together. Prove it by retrieving SysTimeMin on the server itself, then fix the branch that failed - services, storage, provider or upgrade components - and leave display bindings alone.

    Edge computing and IIoT

  • Troubleshooting

    Ignition historian create table fails after a disabled restore

    The double in the failing CREATE TABLE means the MSSQL JDBC driver has no translator selected: a restore with the disable option checked disabled every translator, so generic type names reach SQL Server unchanged and the statement fails to parse. Enable the translators, select the MSSQL translator under Connections > Databases > Settings, restart the gateway, and the sqlt_data partition table creates.

    Edge computing and IIoT

  • Troubleshooting

    Aggregation Matrix report fails when tag count rises

    The failing report is the bigger aggregate workload even though it covers a shorter time range: report size follows tags times intervals, not the date span. The error name identifies the database function that was executing, not the cause. Capture the complete SQL message and test one report setting at a time.

    Edge computing and IIoT

What an internal rental license means on a historian MLFB