ODBC, SQL Server: PROP:SQL "select count(...)" returns garbage characters in a STRING field (use a CSTRING)

Applies to: reported on Clarion 8.0.9661 with the ODBC driver against SQL Server Express 10.50.1600.1.

Problem

A file declared DRIVER('ODBC','/TURBOSQL=TRUE') with STRING(255) fields is used for PROP:SQL. After TurboSQL{PROP:SQL} = 'select count (idCla) from dbo.CLNK' and NEXT, the first field holds a couple of odd characters, not the count. With select cast(count (idCla) as varchar(20)) ... the result is right. The same code with a STRING field worked against Oracle through ODBC.

Solution

  1. Declare the fields that receive PROP:SQL results as CSTRING (the thread used CSTRING(256) and it fixed the result), and drop the CAST. Both posters who answered keep all their PROP:SQL fields as CSTRING.
  2. Read the value from the first field and let Clarion convert it, for example MyCount = (tsql:F1 * 1.0).
  3. The reason: a STRING takes whatever the backend sends as raw bytes. A count sent as a 4-byte number lands in the STRING as its binary bytes (20356 is 4F84 hex, so the characters 84H and 4FH followed by nulls). A CSTRING is interpreted and converted to the text 20356.

Caveats

  • /TURBOSQL tells the driver not to check that the table exists, not to check the declared column names, and not to ask the server for the column types it would use to convert. The poster of that point put it as "you get what you ask for".
  • The same STRING field worked against Oracle; one poster guessed the Oracle driver is coded differently from the ODBC one. This was not tested in the thread.
  • One poster said the key and the NAME attribute are not needed on such a file.

Today (2026)

Not re-tested. The STRING versus CSTRING behaviour is likely unchanged for the ODBC driver; on a current build a quick check with SELECT 20356 into one field of each type shows it.

Sources: Triada (Jun 2013, the question), John G., Graham D., Jon W. (Jun 2013) in softvelocity.clarion.databasedrivers.