如何从 SQL Server 读取数字/小数/金钱列?

发布于 2025-01-03 06:32:18 字数 571 浏览 1 评论 0原文

我尝试按以下方式通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抠脚大汉 2025-01-10 06:32:18

好的,本文解释了这个问题。该解决方案非常麻烦,以至于我决定完全避免使用 SQL_NUMERIC_STRUCT ,受到 这篇文章。现在,我指定 SQL_C_WCHARSQL_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 specify SQL_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文