简体   繁体   English

一次调用即可执行多个SQL语句

[英]Execute multiple SQL statements in one call

I have an update SQL such like ' UPDATE T SET d=d*2 ', then query the updated value such like ' SELECT d FROM T '. 我有一个更新SQL,例如' UPDATE T SET d=d*2 ',然后查询更新的值,例如' SELECT d FROM T '。 Is it possible to use one SQL call to implement this in JDBC? 是否可以使用一个 SQL调用在JDBC中实现此功能? Thanks! 谢谢!

No, mixing DML with SELECT queries is already not possible in plain SQL, so JDBC can't do any much for you. 不,在纯SQL中已经不可能将DML与SELECT查询混合使用,因此JDBC不能为您做任何事情。 You need to fire at least two queries, if necessary in a single transaction. 您需要在单个事务中至少触发两个查询。 An alternative is a stored procedure which you can then exeucte by a single CallableStatement , but that's overcomplicated for this particular simple purpose. 另一种选择是存储过程,然后可以通过单个CallableStatement来执行该存储过程,但是对于此特定的简单用途而言,该过程过于复杂。

You can use Oracle's RETURNING INTO Clause to get the result into a variable in pl/sql. 您可以使用Oracle的RETURNING INTO子句将结果转换为pl / sql中的变量。 Something like this inside pl/sql, but achieving the same result using simple SQL might not be possible. 在pl / sql中使用类似的方法,但是可能无法使用简单的SQL达到相同的结果。

By the way, what database are you using? 顺便说一句,您正在使用什么数据库?

SQL> declare
  2     l_empno number := 7369;
  3     l_sal_initial number;
  4     l_sal_updated number;
  5  begin
  6     select sal
  7        into l_sal_initial
  8        from emp
  9        where empno= l_empno;
 10     dbms_output.put_line('initial sal is.. ' || l_sal_initial);
 11  
 12     update emp 
 13         set sal = sal*2
 14         where empno = l_empno
 15         returning sal into l_sal_updated;
 16  
 17     dbms_output.put_line('final sal is ...' || l_sal_updated);
 18  
 19     rollback;
 20  end;
 21  /

PL/SQL procedure successfully completed.

SQL> set serveroutput on;
SQL> /
initial sal is.. 800
final sal is ...1600

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM