0

I am using the following SQL insert statement to insert values into a table MY_DATA, but here I want to add a check if the row already exists, based on data in column CREATED_AT.

INSERT INTO MY_DATA (SITE_ID, USER_NAME, USER_NO, CREATED_AT) 
VALUES (:siteId, :userName, :userNo, :createdAt)

Here if I try to insert the row with same value present in CREATED_AT column in the table it should not get inserted.

Using Oracle SQL developer for Database.

Please help I am new here.

2
  • 1
    I think you can create a unique index on that column. Commented Oct 15, 2022 at 18:04
  • Hi can you please explain it. Imagine index creation done in table for the column CREATED_AT. How that is going to help and what needs to change in query after that. Commented Oct 15, 2022 at 18:23

3 Answers 3

1

Use a MERGE statement:

MERGE INTO MY_DATA dst
USING DUAL
ON (dst.created_at = :created_at)
WHEN NOT MATCHED THEN
 INSERT (SITE_ID, USER_NAME, USER_NO, CREATED_AT)
 VALUES (:siteId, :userName, :userNo, :createdAt);

You can create an index to prevent multiple insertions with the same CREATED_AT value:

CREATE UNIQUE INDEX my_data__created_at__u ON my_data (created_at);

If you have that, then you can use your query in a PL/SQL statement and catch the exception (and then ignore it, report it or do whatever is required with it):

BEGIN
  INSERT INTO MY_DATA (SITE_ID, USER_NAME, USER_NO, CREATED_AT) 
  VALUES (:siteId, :userName, :userNo, :createdAt);
EXCEPTION
  WHEN DUP_VAL_ON_INDEX THEN
    -- Handle the exception
    NULL;
END;
/
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you I am used merge statement and it's worked. However if I need to add a same condition for other column in future how to include here.
@VinodSP Just add it to the ON clause ON (dst.created_at = :created_at AND dst.user_no = :userNo) or for the index CREATE UNIQUE INDEX my_data__created_at__u ON my_data (user_no, created_at);
1

I think you only need to create a UNIQUE INDEX on your table on CREATE_AT column -

CREATE UNIQUE INDEX idx_unq_created_at
ON MY_DATA (created_at);

After that you don't need to do anything in your query. This index will take care that no duplicate values are being inserted into created_at column in your table.

1 Comment

Thank you for your valuable inputs this will also works but in my case there are already duplicate records present in the column so can't create unique index, and also I don't have right to modify the data in table.
1

One option is to use merge, e.g.

merge into my_data a
  using (select :siteId    site_id, 
                :userName  user_name, 
                :userNo    user_no, 
                :createdAt created_at
         from dual
        ) b
on (a.created_at = b.created_At)
when not matched then 
  insert (site_id, user_name, user_no, created_at)
  values (b.site_Id, b.user_Name, b.user_No, b.created_At);

2 Comments

Thank you it's worked. However if I need to add a same condition for other column in future how to include here.
Just add it to ON clause, e.g. (on (a.created_at = b.created_at and a.site_id = b.site_id and ...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.