gpt4 book ai didi

oracle - 检查INSERT是否成功

转载 作者:行者123 更新时间:2023-12-02 05:56:45 24 4
gpt4 key购买 nike

我有一些执行 INSERT 语句的过程:

CREATE OR REPLACE PROCEDURE potok_insert(
p_jfplate IN potok.jfplate%TYPE,
p_post IN potok.post%TYPE,
p_jfchan IN potok.jfchan%TYPE,
p_jfdatetime IN VARCHAR2
)
AS
t_jfdatetime TIMESTAMP:=TO_TIMESTAMP(p_jfdatetime,'DD.MM.YYYY HH24:MI:SS');
BEGIN
INSERT INTO potok (jfplate, post, jfchan, jfdate_y, jfdate_m, jfdate_d, jftime, jfdatetime,
dt_reg, ibd_arx)
VALUES (RTRIM(p_jfplate),
p_post,
RTRIM(p_jfchan),
EXTRACT(YEAR FROM t_jfdatetime),
EXTRACT(MONTH FROM t_jfdatetime),
EXTRACT(DAY FROM t_jfdatetime),
LPAD(EXTRACT(HOUR FROM t_jfdatetime),2,'0')||':'||
LPAD(EXTRACT(MINUTE FROM t_jfdatetime),2,'0')||':'||
LPAD(EXTRACT(SECOND FROM t_jfdatetime),2,'0'),
CAST(t_jfdatetime AS DATE),
SYSDATE,
1);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END potok_insert;

一些触发器和约束应用于表,它们可以中断插入。如何检查程序主体 - INSERT 是否成功?

当然,我可以在程序的头部和末尾调用 count(),但这不是那么优雅的解决方案。

最佳答案

您可以使用 RETURNING 子句返回刚刚创建的行的 rowid,如下所示:

CREATE SEQUENCE seq_emp;

set serveroutput on

DECLARE
x emp.empno%TYPE;
BEGIN
INSERT INTO emp
(empno, ename)
VALUES
(seq_emp.NEXTVAL, 'Morgan')
RETURNING empno
INTO x;

dbms_output.put_line(x);
END;
/

DECLARE
r rowid;
BEGIN
INSERT INTO emp
(empno, ename)
VALUES
(seq_emp.NEXTVAL, 'Morgan')
RETURNING rowid
INTO r;

dbms_output.put_line(r);
END;
/

DECLARE
x emp.empno%TYPE;
r rowid;
BEGIN
INSERT INTO emp
(empno, ename)
VALUES
(seq_emp.NEXTVAL, 'Morgan')
RETURNING rowid, empno
INTO r, x;

dbms_output.put_line(r);
dbms_output.put_line(x);
END;

取自此链接:

http://www.psoug.org/reference/insert.html

关于oracle - 检查INSERT是否成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1965644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com