使用选择和参数执行 DB2 插入

发布于 2024-09-16 22:08:40 字数 522 浏览 27 评论 0原文

我想做这样的事情:

INSERT INTO TABLEA
(
 COLUMN1, COLUMN2, COLUMN 3
)
SELECT FOOBAR, DOOBAR, ?
FROM TABLEB

然后通过 Spring JDBC 将其发送到 JDBC 进行更新...

simpleJdbcTemplate.update( mySqlFromAbove, someVariableToReplaceQuestionMark );

这可能吗?如果我在构建 SQL 查询时将问号替换为硬编码值,效果会很好,但我不想让自己接受 SQL 注入...

编辑 -
我明白
嵌套异常为 com.ibm.db2.jcc.c.SqlException:DB2 SQL 错误:SQLCODE:-418,SQLSTATE:42610,SQLERRMC:null
这似乎表明
参数标记的使用无效?

I want to do something like this:

INSERT INTO TABLEA
(
 COLUMN1, COLUMN2, COLUMN 3
)
SELECT FOOBAR, DOOBAR, ?
FROM TABLEB

And then send this to JDBC via Spring JDBC to update...

simpleJdbcTemplate.update( mySqlFromAbove, someVariableToReplaceQuestionMark );

Is this even possible? It would work fine if I replace the question mark with the hardcoded value when building my SQL query, but I don't want to open myself to SQL injection...

Edit -
I get
nested exception is com.ibm.db2.jcc.c.SqlException: DB2 SQL error: SQLCODE: -418, SQLSTATE: 42610, SQLERRMC: null
Which seems to indicate
Invalid use of a parameter marker ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

少女净妖师 2024-09-23 22:08:40

您需要对参数标记进行类型转换,以便 DB2 知道会发生什么。

例如:

INSERT INTO TABLEA
(
 COLUMN1, COLUMN2, COLUMN 3
)
SELECT FOOBAR, DOOBAR, cast(? as int)
FROM TABLEB

显然,转换为适当的类型——int只是一个例子。

You need to type-cast your parameter marker so DB2 knows what to expect.

For example:

INSERT INTO TABLEA
(
 COLUMN1, COLUMN2, COLUMN 3
)
SELECT FOOBAR, DOOBAR, cast(? as int)
FROM TABLEB

Obviously, cast to the appropriate type -- int is just an example.

悲喜皆因你 2024-09-23 22:08:40

这是 DB2 SQL 消息参考。以下是您检索到的 SQLCODE 和 SQLSTATE 的相关摘录:

SQL0418N

语句包含无效参数标记的使用。

说明:

不能使用非类型化参数标记:

  • 在选择列表中
  • 作为日期时间算术运算的唯一参数
  • 在某些情况下作为标量函数的唯一参数
  • 作为 ORDER BY 子句中的排序键

永远不能使用参数标记:

  • 在非准备好的声明中
  • 在 CREATE VIEW 语句的全查询中
  • 在 CREATE TRIGGER 语句的触发操作中
  • 在 DB2 Query Patroller 捕获的查询中

无法处理该语句。

用户响应:

更正语句的语法。如果不允许无类型参数标记,请使用 CAST 规范为参数标记指定数据类型。

sql代码:-418

sqlstate:42610

不幸的是,这并不能回答你的问题,因为你的 SQL 看起来不错。在谷歌搜索之后,它看起来更像是这样DB2 JDBC 驱动程序根本不使用 PreparedStatement 中的 INSERT INTO ... SELECT ... 语句。目前尚不清楚 SQL 消息参考中是否缺少该内容,或者 JDBC 驱动程序中是否存在错误。

Here's the DB2 SQL Message Reference. Here's an extract of relevance for the SQLCODE and SQLSTATE you retrieved:

SQL0418N

A statement contains a use of a parameter marker that is not valid.

Explanation:

Untyped parameter markers cannot be used:

  • in a SELECT list
  • as the sole argument of a datetime arithmetic operation
  • in some cases as the sole argument of a scalar function
  • as a sort key in an ORDER BY clause

Parameter markers can never be used:

  • in a statement that is not a prepared statement
  • in the fullselect of a CREATE VIEW statement
  • in the triggered action of a CREATE TRIGGER statement
  • in a query captured by DB2 Query Patroller

The statement cannot be processed.

User Response:

Correct the syntax of the statement. If untyped parameter markers are not allowed, use the CAST specification to give the parameter marker a data type.

sqlcode: -418

sqlstate: 42610

Unfortunately this doesn't answer your problem since your SQL seem to look fine. After Googling a bit more it look more like that the DB2 JDBC driver simply doesn't eat INSERT INTO ... SELECT ... statements in a PreparedStatement. It's unclear if that is missing in the SQL Message Reference or a bug in the JDBC driver.

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