473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

BULK INSERT ignores UNIQUE index with IGNORE_DUP_KEY set?

All,

Just want to make sure that I understand what's going on here.

I have a table with IGNORE_DUP_KEY set on a unique, multi-column
index.

What I'm seeing is this:

1) When performing a BULK INSERT, the UNIQUE index is not being
respected and rows which violate the unique index are inserted.

2) When performing a regular INSERT, the UNIQUE index is being
respected and rows which violate the unique index ARE NOT inserted.

Is this expected behavior.

Also, I have some questions, given the index described.

Q1) Will a regular INSERT that attempts to insert duplicate data get
an error back or just a warning?

Q2) How can I set things up so that a BULK INSERT would NOT allow
duplicates to be entered into the table?

Thanks,
Wes Gamble

Jun 22 '07 #1
2 10869
On Jun 22, 5:33 pm, Weyus <w...@att.netwrote:
All,

Just want to make sure that I understand what's going on here.

I have a table with IGNORE_DUP_KEY set on a unique, multi-column
index.

What I'm seeing is this:

1) When performing a BULK INSERT, the UNIQUE index is not being
respected and rows which violate the unique index are inserted.

2) When performing a regular INSERT, the UNIQUE index is being
respected and rows which violate the unique index ARE NOT inserted.

Is this expected behavior.

Also, I have some questions, given the index described.

Q1) Will a regular INSERT that attempts to insert duplicate data get
an error back or just a warning?

Q2) How can I set things up so that a BULK INSERT would NOT allow
duplicates to be entered into the table?

Thanks,
Wes Gamble
I made a logical error that I just realized. The BULK INSERT that I
was doing, in fact did have unique enough data.

However, my question Q1 still stands - what is the behavior of an
INSERT of duplicate data against this type of index - I suspect that
you get a warning
and not an error - is that correct?

Thanks,
Wes

Jun 22 '07 #2
Weyus (we***@att.net) writes:
>Q1) Will a regular INSERT that attempts to insert duplicate data get
an error back or just a warning?
...
However, my question Q1 still stands - what is the behavior of an
INSERT of duplicate data against this type of index - I suspect that
you get a warning and not an error - is that correct?
Yes, SQL Server only emits a warning. However, there is a catch, some
client APIs incorrectly interprets this as an error. If run:

CREATE TABLE blamblam (a int NOT NULL)
CREATE UNIQUE INDEX updix ON blamblam(a) WITH IGNORE_DUP_KEY
go
EXEC master..xp_cmdshell 'ECHO 12 C:\temp\blamblam.txt'
EXEC master..xp_cmdshell 'ECHO 12 >C:\temp\blamblam.txt'
go
BULK INSERT blamblam FROM 'C:\temp\blamblam.txt'
go
SELECT * FROM blamblam
go
DROP TABLE blamblam

I get this output from Mgmt Studio:

Duplicate key was ignored.

(1 row(s) affected)
a
-----------
12

(1 row(s) affected)

But from Query Analyzer, against the same server instance, I get:
Server: Msg 3604, Level 16, State 1, Line 1
Duplicate key was ignored.

a
-----------
12
(1 row(s) affected)
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Sharp | last post by:
I've been doing some experiments with speeding up copying tables of approximately 1 million rows between databases using BCP and BULK INSERT. I noticed that the total time for removing the...
5
by: me | last post by:
I'm also having problems getting the bulk insert to work. I don't know anything about it except what I've gleened from BOL but I'm not seeming to get anywhere...Hopefully there is some little (or...
4
by: Q. John Chen | last post by:
All, What's the difference between a unique contraint and unique? sementically, if you want a column contain unique values, it is a contraint. And an index is for searching/sort. The questions...
6
by: pk | last post by:
Sorry for the piece-by-piece nature of this post, I moved it from a dormant group to this one and it was 3 separate posts in the other group. Anyway... I'm trying to bulk insert a text file of...
2
by: Ford Desperado | last post by:
I've been reading the docs and playing around, but I'm still not getting the difference. For instance, create table a(i int check(i>0)) create table a_src(i int) go create unique index ai on...
9
by: adi | last post by:
Hi all, Hope there is a quick fix for this: I am inserting data from one table to another on the same DB. The insert is pretty simple as in: insert into datatable(field1, field2, field3)...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
8
by: Mangler | last post by:
Can someone point me in the right direction on this? There are 3 fields in the DB: model, rma, bagqty. If the values on a certain row are I530, 1108, 50 and a user goes to input the same data I...
2
by: nano | last post by:
Does sql server have a way to handle errors in a sproc which would allow one to insert rows, ignoring rows which would create a duplicate key violation? I know if one loops one can handle the error...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.