有一个表“EXAMPLE_TABLE”,其中包含两列。第一列“ID”存储值“5555”,第二列“IS_EXIST”存储 char 1 字节“0”。如何创建一个过程,如果该值不存在,则执行“INSERT INTO”,或者如果“ID”与查询中的相同并且“IS_EXIST”== 0,则执行“UPDATE”,或者抛出一些异常,这些异常将在java if 'ID' 相同且 'IS_EXIST' != 0。我考虑了合并和主要插入的方式来解决这个问题。
它必须大约看起来像:
if(ID doesn't exist)
insert into
if(ID exist and IS_EXIST equals 0)
update
else
throw Exception
但是这在程序中会是什么样子呢?
最佳答案
如果您想使用过程而不合并来抛出或引发一些异常,这是一种简单的方法:
procedure PC_INSERT_OR_UPDATE(P_ID number) as
cursor C_1 is
select M.ID,
C.IS_EXIST
from MY_TABLE M
where M.ID = P_ID;
MSG clob;
begin
for C in C_1 loop
begin
if C.ID is null then
insert into MY_TABLE
(ID,
IS_EXIST)
values
(P_ID,
1);
elsif C.ID is not null and C.IS_EXIST = 0 then
update MY_TABLE M
set M.IS_EXIST = 1
where M.ID = P_ID;
else
RAISE_APPLICATION_ERROR(-20001, 'My exception was raised');
end if;
exception
when others then
rollback;
MSG := 'Error - ' || TO_CHAR(sqlcode) || ' - ' || sqlerrm;
end;
end loop;
end;
关于java - Oracle插入或更新或抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30086410/