Construct a sql query to INSERT the record to table only if there is NO such record in table.
Example:
Insert the new person details to PERSON table only when there is no PERSON with ID 1234.
Background:
If there is a limitation on JAVA layer to handle inserts for existing record in table, then we've to rely on SQL statement to handle this.
If you use iBATIS ORM for batch operations, operation might fail for insert if there is already a record in table (person with ID 1234) you are trying to insert!
Solution:
Instead of having plain INSERT statement, build the needed intelligence in SQL using MERGE statement like below:
MERGE INTO PERSON target USING
(SELECT count(1) record FROM PERSON WHERE ID = '1234') source
ON (source.record = 0)
WHEN MATCHED THEN
INSERT
(PERSON_ID, PERSON_NAME)
VALUES (1234,'RAGSETTY')
No comments:
Post a Comment