473,378 Members | 1,447 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,378 software developers and data experts.

INSERT key violation error, SQL Server

I have two similar SQL Server databases each with a table named
Payments. PaymentID is an identity field and the primary, unique, key
in both tables. There is one other key but it is not unique.

The same INSERT command works for one table but not the other. The
error is "...didn't add 1 record due to key violations". Since the
tables apper to be identical as far as I can tell, I suspect the error
message really means something else.

The INSERT command is:

INSERT INTO Payments ( ControlNumber, ContractNumber, CostCode,
Weight, AccruedAmount, PaidAmount, CreationDate )
SELECT 5000, 1, 'CROSS DOCK', 11190, 100, 0, #2/29/2004 21:44:47# ;

When I paste the command into Enterprise Manager's Query Analyzer (and
change the # to ') it runs for both tables.

I can INSERT records with identity fields in other tables in both
databases so there doesn't seem to be a global problem but something
with Payments.

Any ideas?
Nov 12 '05 #1
6 10611
Thanks. I'll try it this evening.

On Tue, 02 Mar 2004 00:07:38 GMT, Chuck Grimsby
<c.*******@worldnet.att.net.invalid> wrote:

Access doesn't always write the best queries... Your query string
should be:

..>...stuff deleted

Nov 12 '05 #2
Do you perhaps have a trigger on the failing table, that is attempting to
insert a record somewhere else?
We're also assuming - of course - that ControlNumber=5000 doesn't already
exist. Test this using a SELECT statement, rather than by viewing the table
data.

-Tom.

"Larry Johnson" <la***@gjerager.com> wrote in message
news:pj********************************@4ax.com...
I have two similar SQL Server databases each with a table named
Payments. PaymentID is an identity field and the primary, unique, key
in both tables. There is one other key but it is not unique.

The same INSERT command works for one table but not the other. The
error is "...didn't add 1 record due to key violations". Since the
tables apper to be identical as far as I can tell, I suspect the error
message really means something else.

The INSERT command is:

INSERT INTO Payments ( ControlNumber, ContractNumber, CostCode,
Weight, AccruedAmount, PaidAmount, CreationDate )
SELECT 5000, 1, 'CROSS DOCK', 11190, 100, 0, #2/29/2004 21:44:47# ;

When I paste the command into Enterprise Manager's Query Analyzer (and
change the # to ') it runs for both tables.

I can INSERT records with identity fields in other tables in both
databases so there doesn't seem to be a global problem but something
with Payments.

Any ideas?

Nov 12 '05 #3
No triggers. ContolNumber is part of a non-unique index, clustered but
not unique. PaymentID is the unique key, but as that is an identity
field, it isn't in the INSERT.
On Mon, 1 Mar 2004 18:00:27 -0700, "Tom van Stiphout"
<no******************@kinetik-it.com> wrote:
Do you perhaps have a trigger on the failing table, that is attempting to
insert a record somewhere else?
We're also assuming - of course - that ControlNumber=5000 doesn't already
exist. Test this using a SELECT statement, rather than by viewing the table
data.

-Tom.

"Larry Johnson" <la***@gjerager.com> wrote in message
news:pj********************************@4ax.com.. .
I have two similar SQL Server databases each with a table named
Payments. PaymentID is an identity field and the primary, unique, key
in both tables. There is one other key but it is not unique.

The same INSERT command works for one table but not the other. The
error is "...didn't add 1 record due to key violations". Since the
tables apper to be identical as far as I can tell, I suspect the error
message really means something else.

The INSERT command is:

INSERT INTO Payments ( ControlNumber, ContractNumber, CostCode,
Weight, AccruedAmount, PaidAmount, CreationDate )
SELECT 5000, 1, 'CROSS DOCK', 11190, 100, 0, #2/29/2004 21:44:47# ;

When I paste the command into Enterprise Manager's Query Analyzer (and
change the # to ') it runs for both tables.

I can INSERT records with identity fields in other tables in both
databases so there doesn't seem to be a global problem but something
with Payments.

Any ideas?


Nov 12 '05 #4
The changes to the query didn't help. Not a surprise since they work
against one table but not both. I'll work with the other suggestions
in the AM. Thanks again.
On Tue, 02 Mar 2004 00:07:38 GMT, Chuck Grimsby
<c.*******@worldnet.att.net.invalid> wrote:

Access doesn't always write the best queries... Your query string
should be:

INSERT INTO Payments ( ControlNumber, ContractNumber, CostCode,
Weight, AccruedAmount, PaidAmount, CreationDate )
VALUES (5000, 1, 'CROSS DOCK', 11190, 100, 0, #2/29/2004 21:44:47#)

Better yet, change the date to ISO format (YYYY-MM-DD hh:nn:ss) and
put quotes around it.

But the above may not help. You may have an extra index or constraint
on the table that's causing the problem, or a constraint that's
declared global to the database, rather then the table.

On Mon, 01 Mar 2004 17:58:15 GMT, Larry Johnson <la***@gjerager.com>
wrote:
I have two similar SQL Server databases each with a table named
Payments. PaymentID is an identity field and the primary, unique, key
in both tables. There is one other key but it is not unique.
The same INSERT command works for one table but not the other. The
error is "...didn't add 1 record due to key violations". Since the
tables apper to be identical as far as I can tell, I suspect the error
message really means something else.
The INSERT command is:
INSERT INTO Payments ( ControlNumber, ContractNumber, CostCode,
Weight, AccruedAmount, PaidAmount, CreationDate )
SELECT 5000, 1, 'CROSS DOCK', 11190, 100, 0, #2/29/2004 21:44:47# ;
When I paste the command into Enterprise Manager's Query Analyzer (and
change the # to ') it runs for both tables.
I can INSERT records with identity fields in other tables in both
databases so there doesn't seem to be a global problem but something
with Payments.


Nov 12 '05 #5
On Tue, 02 Mar 2004 04:52:41 GMT, Larry Johnson <la***@gjerager.com>
wrote:

OK, sorry I didn't read well enough to realize the PK field was not in
the INSERT statement.

Have you tried DBCC CHECKTABLE?

Can you tell us more about how you're executing this statement. I'm
assuming a query in Access MDB against a ODBC-linked SQL Server table?
If so, delete the link, compact the db, and recreate the link. I have
seen links go bad if index changes were made after the table was
linked.

-Tom.

No triggers. ContolNumber is part of a non-unique index, clustered but
not unique. PaymentID is the unique key, but as that is an identity
field, it isn't in the INSERT.
On Mon, 1 Mar 2004 18:00:27 -0700, "Tom van Stiphout"
<no******************@kinetik-it.com> wrote:
Do you perhaps have a trigger on the failing table, that is attempting to
insert a record somewhere else?
We're also assuming - of course - that ControlNumber=5000 doesn't already
exist. Test this using a SELECT statement, rather than by viewing the table
data.

-Tom.

"Larry Johnson" <la***@gjerager.com> wrote in message
news:pj********************************@4ax.com. ..
I have two similar SQL Server databases each with a table named
Payments. PaymentID is an identity field and the primary, unique, key
in both tables. There is one other key but it is not unique.

The same INSERT command works for one table but not the other. The
error is "...didn't add 1 record due to key violations". Since the
tables apper to be identical as far as I can tell, I suspect the error
message really means something else.

The INSERT command is:

INSERT INTO Payments ( ControlNumber, ContractNumber, CostCode,
Weight, AccruedAmount, PaidAmount, CreationDate )
SELECT 5000, 1, 'CROSS DOCK', 11190, 100, 0, #2/29/2004 21:44:47# ;

When I paste the command into Enterprise Manager's Query Analyzer (and
change the # to ') it runs for both tables.

I can INSERT records with identity fields in other tables in both
databases so there doesn't seem to be a global problem but something
with Payments.

Any ideas?


Nov 12 '05 #6
The solution is to give the role public full permissions on all
tables. Your suggestion about a global issue made me look at it again.

The first SQL Server user for this app, Swift & Co (the big beef
company) has a complex set of users/roles/permissions and I couldn't
figure out how their DBA set it up.

Thanks for the help.

On Tue, 02 Mar 2004 00:07:38 GMT, Chuck Grimsby
<c.*******@worldnet.att.net.invalid> wrote:

Access doesn't always write the best queries... Your query string
should be:

INSERT INTO Payments ( ControlNumber, ContractNumber, CostCode,
Weight, AccruedAmount, PaidAmount, CreationDate )
VALUES (5000, 1, 'CROSS DOCK', 11190, 100, 0, #2/29/2004 21:44:47#)

Better yet, change the date to ISO format (YYYY-MM-DD hh:nn:ss) and
put quotes around it.

But the above may not help. You may have an extra index or constraint
on the table that's causing the problem, or a constraint that's
declared global to the database, rather then the table.


Nov 12 '05 #7

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

Similar topics

5
by: Boogie El Aceitoso | last post by:
Hi, The code below produces an access violation error, complaining that an object is destroyed twice. I don't understand why this happens. Any help would be appreciated. ...
11
by: muser | last post by:
In the code I supplied before this one, the cause of the problem is an access violation error. When I run the debugger it skips into what I can only assume is the compilers version of my code. And...
2
by: Karunakararao | last post by:
Hi All I'm using sybase database, I have a binary datatype column in a table, when I'm trying to store the char(128) value into that, I'm getting syntax violation error. If I take "w" as the...
4
by: Jerry | last post by:
Hi, I have an app which retrieves data from a sql server table and displays it on a datagrid. If 2 sessions of this app are running and 2 users try to update the same record at about the same...
2
by: Agnes | last post by:
I got a simple form and using databinding manager to do the add new Now , my big trobule is . I can update the 'addnew' record, However, after I new the record, and then amend it , it got...
7
by: Tom | last post by:
Hello all: I have a method that does a POST to a secured website using HttpWebRequest. It worked when logging in the site, but it failed with an HTTP prococol violation error when it is used to...
1
by: Khadim | last post by:
I m using HTTWebResponse which is running smoothly on my system which is behing a proxy server. When I run the application with Live IP it gives "HTTP Protocol violation error" I can't use...
2
by: technocraze | last post by:
Hi guys, I have encountered this error when updating the values to the MS Acess table. Error : Update on linked table failed. ODBC sql server error Timeout expired. MS Acess is my front end and...
1
by: gnanapoongothai | last post by:
i have a program to calculate the CRC for a group of array values. SO i have program in a different file and i am passing the length and the array to the function and i am getting a access voilation...
2
by: jthep | last post by:
I'm trying to get this piece of code I converted from C to work in C++ but I'm getting an access violation error. Problem occurs at line 61. Someone can help me with this? The function...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.