【问题标题】:SQL Query for executing a series of update statements用于执行一系列更新语句的 SQL 查询
【发布时间】:2024-04-30 17:50:01
【问题描述】:

我有一组更新语句,需要一个接一个地执行。我使用的数据库是 Oracle,我通过使用 shell 脚本连接到数据库来运行这些查询。我在这里想知道的是,只有在前一个更新语句成功的情况下,我才能一个接一个地执行这些命令。自动更新在 shell 脚本中设置为关闭,我只想在所有语句都成功执行时提交所有语句,否则我想退出。有没有办法通过检查前面的查询状态并在所有查询成功运行后提交来依次运行这些更新命令。

【问题讨论】:

  • 您使用的是 Oracle 还是 MySQL?
  • 使用 if else 阻塞
  • 是oracle DB
  • update * from table_name where condition;
  • update * from table_name where condition;update * from table_name where condition;update * from table_name where condition;update * from table_name where condition;update * from table_name where condition;

标签: sql oracle shell unix


【解决方案1】:

你可以使用 WHENEVER SQLERROR EXIT ROLLBACK:

sqlplus -s username/password@network-name <<EOF
   WHENEVER SQLERROR EXIT ROLLBACK
   update table1 set mycolumn=value where mycolumn=1;
   update table1 set mycolumn=othervalue where mycolumn=2;
   update table1 set mycolumn=othervalue where mycolumn=3;
   update table1 set mycolumn=othervalue where mycolumn=4;
   commit;
   exit
EOF

这里是文档的链接:

https://docs.oracle.com/database/121/SQPUG/ch_twelve052.htm

【讨论】:

  • 这正是我想要的。谢谢队友:)
  • 太棒了!如果它解决了您的问题,请考虑接受答案。