如何从 SQL Server 读取数字/小数/金钱列?
我尝试按以下方式通过 ODBC 从 SQL Server 读取数字/小数/货币列:
SQL_NUMERIC_STRUCT decimal;
SQLGetData(hSqlStmt, iCol, SQL_C_NUMERIC, &decimal, sizeof(decimal), &indicator);
所有这些类型都作为 SQL_NUMERIC_STRUCT 结构返回,并且我将 SQL_C_NUMERIC 类型指定为 SQLGetData() API。
在数据库中,列定义为例如decimal(18, 4)
,或money
。但问题是,返回的数据的 decimal. precision
始终设置为 38(最大可能值),而 decimal.scale
始终设置为零。因此,如果实际存储的数字是12.65,则SQL_NUMERIC_STRUCT结构中的返回值等于12。因此,出于一个非常奇怪的原因,小数部分被简单地丢弃了。
我可能做错了什么?
I try to read numeric/decimal/money columns from SQL Server via ODBC in the following manner:
SQL_NUMERIC_STRUCT decimal;
SQLGetData(hSqlStmt, iCol, SQL_C_NUMERIC, &decimal, sizeof(decimal), &indicator);
All these types are returned as SQL_NUMERIC_STRUCT structure, and I specify the SQL_C_NUMERIC type to SQLGetData() API.
In the database, column is defined as decimal(18, 4)
for example, or money
. But the problem is that the returned data has decimal.precision
always set to 38 (max possible value) and decimal.scale
always set to zero. So if the actual stored number is 12.65, the returned value in SQL_NUMERIC_STRUCT structure is equal to 12. So the fractional part is simply discarded, for a very strange reason.
What can I be doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,本文解释了这个问题。该解决方案非常麻烦,以至于我决定完全避免使用
SQL_NUMERIC_STRUCT
,受到 这篇文章。现在,我指定SQL_C_WCHAR
(SQL_C_CHAR
也可以)并直接将数字/小数/货币列读取为文本字符串。看来驱动程序进行了转换。OK, this article explains the problem. The solution is so cumbersome, that I decided to avoid using
SQL_NUMERIC_STRUCT
altogether, influenced by this post. Now I specifySQL_C_WCHAR
(SQL_C_CHAR
would do as well) and read the numeric/decimal/money columns as text strings directly. Looks like the driver does the conversion.