最近再移植一个从oracle转mysql的项目,喜提一个报错:
You can't specify target table 'A016' for update in FROM clause
对应的程序代码:
public void setCurrent(String setId, String pk, String userId) throws SysException {
String[] sql = new String[2];
sql[0] = "update " + setId + " set " + setId + "000='00900' where id in (select id from " + setId + " where subid='" + pk + "')";
sql[1] = "update " + setId + " set " + setId + "000='00901' where subid='" + pk + "'";
api.batchExecuteSql(sql);
}
sql[0]报的错。在oracle sqlserver里都没有错。mysql不行。核心问题是mysql不允许在update的where语句里出现要更新的表。
针对这种情况只能先把值取出来,然后在执行。
public void setCurrent(String setId, String pk, String userId) throws SysException {
String id=api.queryForString("select id from " + setId + " where subid='" + pk + "'");
String[] sql = new String[2];
sql[0] = "update " + setId + " set " + setId + "000='00900' where id ='"+id+"'";
sql[1] = "update " + setId + " set " + setId + "000='00901' where subid='" + pk + "'";
api.batchExecuteSql(sql);
}


![[论文阅读]Formalizing and Benchmarking Prompt Injection Attacks and Defenses](https://i-blog.csdnimg.cn/direct/cf94b64de14244e5b1f10f8f8525fa7e.png)













![[Java实战]Spring Boot 快速配置 HTTPS 并实现 HTTP 自动跳转(八)](https://i-blog.csdnimg.cn/direct/d44fad31319344acad6e275a0a2c2f40.png#pic_center)


