0

I have a table with primary Key. When I am running an Insert statement it gives an errror where it finds a duplicate row and stops the insertion. I want to Insert the data into the table by skipping the duplicate row. Can anyone please suggest me how to do this.

Thanks

vinisha9
  • 91
  • 1
  • 2
  • 8
  • Insert from query wich returns only distinct rows from the source data. Kinda `INSERT ... SELECT DISTINCT ...` – Serg May 18 '16 at 13:41
  • Look a this [question](http://stackoverflow.com/questions/2513174/how-to-avoid-duplicates-in-insert-into-select-query-in-sql-server) – ZeusNet May 18 '16 at 13:41

3 Answers3

0

If you work with MySQL database then your primary key column has to be of AUTO_INCREMENT type and MySQL will automatically insert new value for PK. If you work with ORACLE, you have to use SEQUENCE as different method which will increment and give you new value for PK.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
sbrbot
  • 6,169
  • 6
  • 43
  • 74
0

You haven't tagged which engine you are using so I don't know if this will work for you.

The MERGE statement combines an INSERT/UPDATE/DELETE into a single unit of work.

Stavr00
  • 3,219
  • 1
  • 16
  • 28
0

Your table primary key column has to be of AUTO_INCREMENT so that you don't have to insert that value by yourself, it will take automatically.

And also it will be the best practise for you. Because each time you have to insert the unique value so, on behalf of you the database will take care of it.

Ankit Modi
  • 25
  • 1
  • 9
  • The primary key column is a transaction ID column which we will be inserting into. We want this column to be primary key so that any dupliacte transaction Id's will not be inserted into the table. So i cannot really have an AUTO_INCREMENT for this column. Please suggest me any other way of doing this. – vinisha9 May 18 '16 at 15:17
  • find the last largest number inserted in your table and increment it by 1 and then add/insert data in a table, but here you have two calls in a database. 1] To get the largest number 2] To insert data by in a database(id will be largest number + 1) – Ankit Modi May 23 '16 at 06:45