IMD SolutionComputing LibraryShop main store

Troubleshooting

Cicode insert not logging to SQL Server after SQLConnect succeeds

Edge computing and IIoT Published 7 min read

Short answer

SQLConnect returning a handle proves the login, not the insert. When a Cicode insert writes no row, capture the assembled statement immediately before SQLExec and fix the shown defects: Time_1 alone reaches the DateTime column while the combined Time_Date string is discarded, and the execution result is never read. Then rerun the exact string through the same DSN with an ODBC client on the runtime machine — SQL Server Management Studio is not that test.

On this page
  1. What a successful SQLConnect proves
  2. Check the path in this order
  3. What is wrong in the statement as written
  4. Counting the characters DATA_LOCK assembles, with example values
  5. Rerun the captured statement through the same DSN
  6. The decision for each finding
  7. Questions asked

A Cicode function that logs nothing while its connection reports success is failing past the login, and the usual place is the statement itself. The case here is a Vijeo Citect 7.40 project: a button in the graphics builder calls DATA_LOCK(), which connects through the DATA_DB_REPORT DSN and inserts into REPORT_TABLE, yet no row appears. SQLConnect returning a handle proves the account, the driver and the server name; it proves nothing about what SQLExec sends. Capture the assembled statement in the line immediately before SQLExec, confirm the button enters the function, read the execution result, and rerun the exact string through the same DSN with an ODBC client on the runtime machine. Two defects are already visible: only Time_1 reaches the DateTime column while the combined Time_Date string is discarded, and the execution result is declared but never captured.

What a successful SQLConnect proves #

The path from the button to the row crosses three boundaries, and each is a separate operation with its own failure mode.

  • Login. SQLConnect opens the ODBC connection through the DSN, and the Windows data source test proves the same three things: the driver is present, the server is reachable, the account is accepted.
  • Execution. SQLSet and SQLAppend build the statement and SQLExec runs it — the Cicode reference documents SQLExec as executing any SQL query or command the database supports. The connected account still needs permission to insert into the target table, and every submitted value must satisfy the destination column's type and constraints.
  • Destination. The DSN names one database on one server. REPORT_TABLE is a table in that database; connecting does not expose the SCADA project, its tags or its own storage as SQL tables. An insert into a similarly named table in another database looks exactly like a logging failure from the button.

A hand-built Cicode insert is also a different mechanism from the product's own logging paths, which fail for their own reasons — see why Citect alarm logging never reaches SQL Server.

Check the path in this order #

Compile the project and clear every syntax or declaration error before testing the database leg — a function that does not compile never reaches SQL. Then work down the path.

  1. Button to function. Put a trace or on-screen message as the first line of DATA_LOCK() and press the button. No output means the button binding is the fault, not SQL.
  2. Function to values. Display one, two, three and four immediately before conversion. A locally declared REAL is not bound to a controller tag by name; the deciding checks are declaration, scope and the value at the moment of execution.
  3. Values to statement. Capture the complete assembled buffer at the line immediately before SQLExec and read it as one string, character for character. Never substitute a hand-written query for the runtime query during diagnosis; quotes, separators and conversion output can differ silently.
  4. Statement to execution. The declared handle hexec is never used, so the execution result is discarded and a login-level success is indistinguishable from a statement failure. Capture the result the runtime reports and read its native error before assigning a cause.
  5. Execution to destination. Open the exact database the DSN names and look for the row carrying the values and timestamp the statement sent. The button insert writes a row only when the button is pressed; a row count proves nothing about which mechanism wrote it.

What is wrong in the statement as written #

hsql = SQLConnect("dsn=DATA_DB_REPORT");
SQLSet(hsql, "INSERT INTO REPORT_TABLE(DateTime,Data1,Data2,Data3,Data4) VALUES('");
SQLAppend(hsql, Time_1 + "','");
SQLAppend(hsql, RealToStr(one,12,2) + "','");
SQLAppend(hsql, RealToStr(two,12,2) + "','");
SQLAppend(hsql, RealToStr(three,12,2) + "','");
SQLAppend(hsql, RealToStr(four,12,2) + "')");
SQLExec(hsql, "");
SQLDisconnect(hsql);
  • The timestamp fragment is wrong. Date_1 = Date(2) and Time_1 = Date(10) are read, Time_Date = Date_1 + Time_1 is built — and never appended, so the statement inserts Time_1 alone into the DateTime column. Display both fragments in your build before choosing a format; the Cicode conversion reference covers date-time conversions and SQL-database-independent date-time syntax for exactly this step. If the column must hold date and time together, the fragment that belongs in the statement is the combined string.
  • All five values are quoted as text. SQL Server converts quoted text into a numeric column, but only when the text is a clean number within that column's precision and range — so whatever RealToStr(one,12,2) produces, including any padding, must convert. Display the converted fragment instead of predicting it.
  • The result is thrown away. hexec is declared and never used, so the code cannot tell a failed statement from a successful one.

One scope note: four REAL values read at button-press time give the operator a snapshot of that instant, not a history. If the requirement grows into continuous collection, that is an architecture decision rather than a statement fix — selecting SCADA software for mixed PLCs and SQL logging covers how to choose that layer.

Counting the characters DATA_LOCK assembles, with example values #

Inputs: the SQLSet string and the five appended fragments come from the code above; the values are illustrative — Time_1 = 10:15:30 (whatever Date(10) returns in your build, display it), one = 125.5, two = 64, three = 3.25, four = 1000, converting to the six-character 125.50, the five-character 64.00, the four-character 3.25 and the seven-character 1000.00. Print your own fragments rather than trusting these; the width argument's effect must be read off the captured string.

Rule: the database receives exactly the concatenation of those fragments — nothing added, nothing removed — so the statement length is the sum of the fragment lengths, and each column value is whatever sits between its own pair of single quotes.

  • SQLSet fragment INSERT INTO REPORT_TABLE(DateTime,Data1,Data2,Data3,Data4) VALUES(' = 67 characters.
  • Time_1 plus its separator: 8 + 3 = 11; running total 67 + 11 = 78.
  • one: 6 + 3 = 9, total 87.
  • two: 5 + 3 = 8, total 95.
  • three: 4 + 3 = 7, total 102.
  • four: 7 + 2 = 9, total 111.

Result: the statement is 111 characters long — INSERT INTO REPORT_TABLE(DateTime,Data1,Data2,Data3,Data4) VALUES('10:15:30','125.50','64.00','3.25','1000.00') — with ten single quotes and five values. In the same example Time_Date = 10 + 8 = 18 characters of date and time, and none of them reach the database.

Decision: if the captured buffer does not match this reconstruction — same length, ten single quotes, five values — the fault is in the assembly, and the fragments get fixed before the database is touched. If the buffer matches character for character and the insert still fails, the statement is exonerated and the fault moves to the connection context.

Rerun the captured statement through the same DSN #

The test that matches the runtime path uses a third-party ODBC client, on the runtime machine, on the same DATA_DB_REPORT DSN, executing the captured string pasted character for character. SQL Server Management Studio is not that test; a statement that runs there proves nothing about the ODBC leg the runtime travels.

The two outcomes decide the case. If the ODBC client inserts the row, the statement and the database leg are correct and the fault lives in the runtime: the discarded execution result, or the account the runtime runs under — a DSN that tests fine interactively can be invisible to a runtime executing as a service. If the ODBC client fails as well, its native error names the cause directly: a failed conversion, a missing INSERT permission, a table that does not exist in the database the DSN opens.

Run this test against a copy of the table or a scratch database — an INSERT that lands in the live reporting table writes rows the reports will read.

A connection that will not open at all is a different fault family from a statement that never lands; that side, its errors and its fixes are covered in fixing an ODBC SSL security error 08001 on an edge PC.

The decision for each finding #

  • No trace output when the button is pressed: the function never runs — fix the button binding.
  • Trace appears but the values are empty: declaration, scope or assignment — the insert cannot carry values that were never read.
  • Captured statement differs from the intent: fix the fragments — append the timestamp representation the column needs and display every conversion output before trusting it.
  • Statement correct, ODBC client inserts, runtime does not: runtime context or the discarded result — capture the execution result and check which account runs the runtime and which DSN it sees.
  • Statement correct, ODBC client fails too: statement or database side — the client's native error names the layer.

Work the list top down: the first check that fails names the layer to fix, and the captured statement buffer — not the connection dialog — is the evidence at every step.

Questions asked

Why does the ODBC data source test succeed while the Cicode insert still fails?

The Windows DSN test opens the connection, checks the driver, server name and login, and reports success. It never builds or executes an INSERT, so it proves nothing about the statement DATA_LOCK assembles. Login and statement execution are separate operations: the connected account also needs permission to insert into REPORT_TABLE, and every submitted value must satisfy the destination column's type and constraints. Capture the statement and test that operation on its own.

How do I see the exact SQL statement the runtime sends?

Display the buffer as the last step before SQLExec: a trace or message box showing the assembled string at that line gives you the statement character for character. Read it as one line and compare it with what you intended; in the article's example it assembles to 111 characters with ten single quotes and five values. Never substitute a hand-written query during diagnosis, because quotes, separators and conversion output can differ silently.

Can SQL Server Management Studio stand in for the ODBC client test?

No. Management Studio connects with its own client stack and logs in directly to the database engine; it never uses the ODBC driver or the DATA_DB_REPORT DSN that the Cicode runtime uses. A statement that runs there proves nothing about the ODBC leg. Use a third-party ODBC client on the runtime machine, select the same DSN, and execute the captured string exactly as captured.

What ends up in the DateTime column when only Time_1 is inserted?

Whatever text Time_1 holds after Date(10), converted by SQL Server into a datetime. If that text is time-only, SQL Server stores the time on its default date, 1 January 1900; if it is date-only, the time becomes midnight. Display Date_1 and Time_1 in your build, decide which representation the column must hold, and append the fragment that matches. The combined Time_Date string only helps if its format converts.

Why can the runtime fail to see a DSN that tests fine?

Windows keeps two kinds of DSN. A user DSN exists only for the Windows account that created it; if the SCADA runtime runs as a service under a different account, that account may see no DSN or a different one. A system DSN is registered for the machine and is visible to every account on it. If the connection works interactively but not from the runtime, check which kind DATA_DB_REPORT is and under which account the runtime executes.

Keep reading

  • Troubleshooting

    Why Citect alarm logging never reaches SQL Server

    Alarms that populate the runtime page but never reach the SQL table mean the display path works and the logging path does not. Test three links in order: the alarm category's Log Device binding, the ODBC DSN and device connection including DSN bitness, then the INSERT format string. Read the syslog first and trace with SQL Profiler second — no traced statement puts the fault on the SCADA side, a failing statement puts it in the table or format.

    Edge computing and IIoT

  • How-to

    Export selected Geo SCADA historian tags to another historian

    Pull the selected tags through the documented SQL query interface in measured time windows, and write them to the destination through whatever interface that system's documentation accepts; use a customized transfer application only when the destination's ingestion needs application logic. Never read the historic database folder directly: it is a proprietary binary format held locked by the running server.

    Edge computing and IIoT

  • Troubleshooting

    Linking AVEVA Historian to Omron PLC data through IDAS topics

    A topic alone is not the fix. Build the chain: a DeviceSelector and a Device Group in Operations Integration Server Manager against the OMRONFINS device, a Historian topic matching that group for the IDAS and I/O server, then the tag's item address. Prove one test tag end to end before importing the rest.

    Edge computing and IIoT

Log paired Torque and Angle values for a live XY chart