Update 语句中 @po 附近的语法不正确
我使用 Java 中的 preparedStatement 来更新和替换 MSSQL 2008 数据库中的记录,如下所示:
ps = settings.conn.prepareStatement("UPDATE table SET ? = replace((?), (?), (?)) ");
然后我按如下方式传递参数:
String[] columns= {"a", "b", "c", "d"};
for (int i = 0; i < columns.length; i++) {
ps.setString(1, columns[i]);
ps.setString(2, columns[i]);
ps.setString(3, " " + oldName.trim() + " ");
ps.setString(4, " " + newName.trim() + " ");
ps.addBatch();
batchSize++;
if (batchSize > 5000) {
batchSize = 0;
ps.executeBatch();
}
}
我收到很多错误消息,指出@po 附近的语法不正确。从 这个问题 我知道 Top-statement 应该包含在在参数化语句中使用时用括号括起来。
难道 Update 语句还需要一些额外的格式才能使用它吗?或者还有其他问题吗?
I'm using a preparedStatement in Java to update and replace records in a MSSQL 2008 database as follows:
ps = settings.conn.prepareStatement("UPDATE table SET ? = replace((?), (?), (?)) ");
I am then passing in the arguments as follows:
String[] columns= {"a", "b", "c", "d"};
for (int i = 0; i < columns.length; i++) {
ps.setString(1, columns[i]);
ps.setString(2, columns[i]);
ps.setString(3, " " + oldName.trim() + " ");
ps.setString(4, " " + newName.trim() + " ");
ps.addBatch();
batchSize++;
if (batchSize > 5000) {
batchSize = 0;
ps.executeBatch();
}
}
I get a lot of error messages saying incorrect syntax near @po. From this question I understand that the Top-statement should be inclosed in brackets when it is used in a parameterized statement.
Could it be that the Update statement also needs some additional formatting before I can use it? Or is something else going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如前所述,列名不是绑定参数。语句中的动态列名更接近于动态sql。如果语句的结构是动态的,则sql server无法真正解析和准备语句。
看起来这个例子试图实现这样的目标:
如果目的不是更新所有列,而只是更新其中的一些列,则可以执行如下操作:
如果列的输入参数为 NULL,则设置列值等于当前值(即没有真正修改),否则将列值设置为计算结果。 (Oracle 语法)
As mentioned earlier, a column name is not a bind parameter. A dynamic column name in the statement is closer to dynamic sql. The sql server can't really parse and prepare the statement if the structure of the statement is dynamic.
Looks like the example is trying to achieve something like this:
If the intention was not to update all columns, but only some of them, you could do something like this:
If the input parameter for a column is NULL, then set the column value equal to the same as what it currently is (ie don't really modified), otherwise set the column value to calculated result. (Oracle syntax)
我不相信您可以使用这样的参数指定列名称 - 该部分需要是 SQL 本身的一部分。只能参数化值。
I don't believe you can specify the column name using a parameter like this - that part needs to be part of the SQL itself. Only values can be parameterized.