执⾏Oracle的insert或update语句时同时返回插⼊或更新后的
字段值
执⾏Oracle的insert或update语句时同时返回插⼊或更新后的字段值
这是⼀个很有⽤并常⽤的语法,可以少⼀次查询。
insert:
CREATE SEQUENCE seq_emp;
SET SERVEROUTPUT ON
DECLARE  pno%TYPE; BEGIN  INSERT INTO emp  (empno, ename)  VALUES  (seq_emp.NEXTVAL, 'Morgan')  RETURNING empno  INTO x;
dbms_output.put_line(x); END; /
DECLARE  x urowid; BEGIN  INSERT INTO emp  (empno, ename)  VALUES  (seq_emp.NEXTVAL, 'Morgan')  RETURNING rowid  INTO x;
dbms_output.put_line(x); END; /
update:
conn hr/hr
var bnd1 NUMBER  var bnd2 VARCHAR2(30)  var bnd3 NUMBER
UPDATE employees  SET job_id ='SA_MAN', salary = salary + 1000, department_id = 140  WHERE last_name = 'Jones'  RETURNING salary*0.25, last_name, department_id  INTO :bnd1, :bnd2, :bnd3;
insert语句字段顺序
print bnd1  print bnd2  print bnd3 conn hr/hr
variable bnd1 NUMBER
UPDATE employees  SET salary = salary * 1.1  WHERE department_id = 100  RETURNING SUM(salary) INTO :bnd1;
print bnd1

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。