【问题标题】:Invalid character value for cast specification on order of sql selectsql 选择顺序上的强制转换规范的字符值无效
【发布时间】:2020-03-27 12:57:48
【问题描述】:

我有这个数据(简化)

当我按该顺序选择int1, int2, str1 时,我得到一个无效的转换错误。当我在str1, int1, int2 order 中选择它们时,sql 调用成功完成。

sql 调用如下所示(再次简化)

Select int1, int2, str1 from table where int1 = ? (my variable)

在此调用中没有实际的值转换。

代码是 c++,使用 Native Client 11 的 ODBC 连接到 MSSQL 数据库。 变量和绑定列都是静态声明的,并且具有适当的类型和长度(并且按照我更改顺序时的调用顺序)。

我正在寻找任何其他想法,以了解当我在 int 值之后选择 char 值时如何找到它失败的原因。失败发生在SqlFetch函数调用(返回的数据试图加载到绑定列时)

非简化查询返回 140 列混合类型,我想将解决方案应用于整个查询。

(注意) c++ 列绑定看起来像这样,按选择的顺序排列:

sqlret = SQLBindCol(hstmt_myQuery,item_col,SQL_C_LONG,&int1, (SDWORD) sizeof(int1), &indicator[item_col++]); exec_sql_error(__FILE__, &henv, &hdbc, last_connect_time, "BindCol hstmt_myQuery", hstmt_myQuery, &sqlret);
sqlret = SQLBindCol(hstmt_myQuery,item_col,SQL_C_LONG,&int2, (SDWORD) sizeof(int2), &indicator[item_col++]); exec_sql_error(__FILE__ , &henv, &hdbc, last_connect_time , "BindCol hstmt_myQuery" , hstmt_myQuery , &sqlret  );
sqlret = SQLBindCol(hstmt_myQuery,item_col,SQL_C_CHAR,str1, (SDWORD) sizeof(str1), &indicator[item_col++]); exec_sql_error(__FILE__ , &henv, &hdbc, last_connect_time , "BindCol hstmt_myQuery" , hstmt_myQuery , &sqlret  );

【问题讨论】:

    标签: c++ sql-server


    【解决方案1】:

    参数的求值顺序未指定。

    所以你对item_colSQLBindCol(/*..*/, item_col, /*..*/, item_col++); 有问题。

    在单独的语句中增加 item_col 应该可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 2022-10-19
      相关资源
      最近更新 更多