We try to use java preparedstatement to do insert action, we got ORA-00001: unique constraint (POSTIONS_TABLE_PK) violated error.
The app target at Oracle DB. the table name info:
postion_table columns:
position_id number pk,
amount number,
account_id number
We need postion_id to be sequential number. start from 1. so will be 1, 2 ,3 .... n. For example, when we delete record with position_id 10, then the next new insert need to use 10 as position_id.
The insert query for prepared statement looks like:
BEGIN
DECLARE
position_id number;
BEGIN
SELECT NVL(MAX(t.position_id) + 1, 1)
INTO position_id
FROM positions_table t
WHERE account_id = 1;
INSERT into positions_table(position_id,amount, account_id)
values(position_id, ?, ?);
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20999, 'Error : ' || sqlerrm);
ROLLBACK;
END;
END;
We got unique constraint (TPOSTIONS_TABLE_PK) violated error when we try to insert multiple records in a batch.
Any suggestion on how to build the prepared SQL.
rownum(sorted by insert time) can be inferred as actual position - PrasadU