Solved

sql equivelent of Insert Ignore

Posted on 2010-11-18
3
832 Views
Last Modified: 2012-05-10
sorry, I saw angellll response to this question but I guess I need further clarification.  Using MSSQL 2005, I want to execute the following command and have it ignore duplicate key values:

Insert Into "Sample Database".dbo."customer notes"
("Customer ID", "Note Date", Text)
Select "Customer ID", "Noted Date", Text
From "Test Database".dbo."customer notes"

The customer ID has duplicate values, but I want to ignore this.  What specifically is the syntax of the SQL statement?
0
Comment
Question by:medpipes
3 Comments
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 34164724
try

Insert Into "Sample Database".dbo."customer notes"
("Customer ID", "Note Date", Text)
Select "Customer ID", "Noted Date", Text
From "Test Database".dbo."customer notes"
where "Customer ID" not in (select "Customer ID" from "Sample Database".dbo."customer notes")
0
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 34164765
It depends how you want to handle it.
Your select statement that provides the data could do a group by...
e.g.

Insert Into "Sample Database".dbo."customer notes"
("Customer ID", "Note Date", Text)
Select "Customer ID",  Max("Noted Date"), Max(Text)
From "Test Database".dbo."customer notes"

However, the date and text may come from different records so:

Insert Into "Sample Database".dbo."customer notes"
("Customer ID", "Note Date", Text)
Select "Customer ID", "Noted Date", Text
FROM
(
 Select "Customer ID", "Noted Date", Text,
RowNumber() OVER (PARTITION BY "Customer ID" ORDER BY "Noted Date" DESC) as rn
 From "Test Database".dbo."customer notes"
) ilv
where rn=1

This returns the date and text for the most recent customer note for each customer

0
 

Author Closing Comment

by:medpipes
ID: 34165764
This worked and was exactly what I needed - thank you!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

823 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