Using oracle 12c. Here is my procedure for save or update which should return id in case of saving new record:
procedure save_dsc (p_id in out integer,
p_caption_name in varchar2,
p_dat_id in integer,
p_dsc_id in out integer)
is
begin
if p_dsc_id is null
then
select c.id
into p_dsc_id
from pod_data_set_category c
where c.dat_id = p_dat_id and dsc_id is null;
end if;
if p_id is null
then
insert into pod_data_set_category (id,
caption_name,
dat_id,
dsc_id)
values (pod_dsc_seq.nextval,
p_caption_name,
p_dat_id,
p_dsc_id)
returning id
into p_id;
else
update pod_data_set_category
set caption_name = p_caption_name,
dsc_id = p_dsc_id
where id = p_id;
end if;
end;
and here is java code:
try (CallableStatement stmt = connection.prepareCall("{call save_dsc(?,?,?,?)}")) {
stmt.registerOutParameter(1, Types.INTEGER);
if (entryCategoryBean.getId() != null) {
log.debug("Update");
stmt.setInt(1, entryCategoryBean.getId());
stmt.setNull(3, Types.NULL);
} else {
log.debug("Create");
stmt.setNull(1, Types.INTEGER);
stmt.setInt(3, entryCategoryBean.getDatasetId());
}
stmt.setString(2, entryCategoryBean.getName());
if (entryCategoryBean.getParentId()!= null) {
stmt.setInt(4, entryCategoryBean.getParentId());
}
else {
stmt.setNull(4,Types.NULL);
}
stmt.registerOutParameter(4, Types.INTEGER);
stmt.execute();
if (entryCategoryBean.getId() == null) {
entryCategoryBean.setId(stmt.getInt(1));
}
}
Either I do save or update i get java.sql.SQLException: Parameter Type Conflict. How to fix that?
FIXED this way:
procedure save_dsc (p_id in out integer,
p_caption_name in varchar2,
p_dat_id in integer,
p_dsc_id in integer)
is
v_dsc_id integer :=p_dsc_id;
begin
if (p_dsc_id is null)
then
select c.id
into v_dsc_id
from pod_data_set_category c
where c.dat_id = p_dat_id and dsc_id is null;
end if;
if p_id is null
then
insert into pod_data_set_category (id,
caption_name,
dat_id,
dsc_id)
values (pod_dsc_seq.nextval,
p_caption_name,
p_dat_id,
v_dsc_id)
returning id
into p_id;
else
update pod_data_set_category
set caption_name = p_caption_name,
dsc_id = v_dsc_id
where id = p_id;
end if;
end;
Maybe I'm completely wrong, but you might try to use local variables instead of using the in/out parameters directly.
...
IS
v_dsc_id INTEGER;
BEGIN
IF (p_dsc_id IS NULL) THEN
SELECT c.id
INTO v_dsc_id
....;
p_dsc_id := v_dsc_id;
END IF;
...
Same story of course for the p_id
parameter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.