Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Error handling in Insert

Posted on 2013-01-31
3
Medium Priority
?
166 Views
Last Modified: 2013-01-31
Hello Experts!!

I am trying to load a table using Insert statement.

My script will insert fine for the first time and when I run the same script once again, I ll get an error saying cannot insert duplicate values ( I have two columns and both are primary keys, so I cant insert duplicate values )

Is there a way to re-write it so that when I insert the second time and if there are any new records, it will insert them and the rest wont be inserted.

CAN WE DO THIS WITHOUT USING UPSERT/MERGE ??

Thanks!!!
0
Comment
Question by:ravichand-sql
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Accepted Solution

by:
Jared_S earned 2000 total points
ID: 38840404
Why not just eliminate the records prior to insert by using a NOT EXISTS statement against your table?

INSERT INTO Table1

SELECT * FROM Table2
WHERE NOT EXISTS
(SELECT null FROM Table1 WHERE Table1.Key = Table2.Key)
0
 
LVL 40

Expert Comment

by:lcohan
ID: 38840693
Same idea but using an EXCEPT sql function:

INSERT INTO Table1
SELECT * FROM Table2
EXCEPT
SELECT * FROM Table1 WHERE Table1.RowKey = Table2.RowKey
0
 

Author Comment

by:ravichand-sql
ID: 38840853
Thanks, it helped!!
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question