SQL Server - INSERT throws duplicate error but the row is INSERTED - sql

I have an INSERT statement that throws the Violation of Primary Key error but the rows is inserted into the database successfully.
Violation of PRIMARY KEY constraint 'PK_TableName_ID'. Cannot insert duplicate key in object 'dbo.TableName'. The duplicate key value is (ID).
Prior to the insert statement, I check if the ID exists in the table. If it doesn't, I proceed with the INSERT statement.
In the table, I have a DATETIME column, InsertedAt, which equals to the time when the rows was inserted.
I have no idea why the error is thrown and would love to hear other potential causes.
Thank you.

Related

How can I INSERT into this table?

I have set a trigger up which I need to test. To do this I need to carry out an insert into the tblSOLine table however when I do this I am getting an error which I assume relates to the way the PK/FKs are set up. Is there any way I could do an insert into this table without messing with the table relationships?
Here is what I tried:
INSERT INTO tblSOLine (SOHeaderID, ProductID, Quantity, NetSale, VAT)
VALUES (5364, 6, 6, NULL, NULL)
Here is the error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tblSOLine_tblSOHeader".
The conflict occured in database "VintageSounds", table "dbo.tblSOHeader", column 'SOHeaderID'.
The error is saying that SOHeaderID = 5364 does not exist in SOHeader. You need to be sure that this value is what you intend.
You should not be getting an error if you attempt to insert this a headerid with this number, if the foreign key does not work. However, such ids are usually IDENTITY columns, so they are automatically generated. If you don't have a typo in the number, then perhaps you are confusing the number with something else, such as the customer number.

Insert into violation of PRIMARY or UNIQUE KEY constraint Firebird 2.5

I am trying to insert a few values in an existing table, but first it makes nothing and 2nd time it shows me this error message.
My Unique KEY consists of a FK of a higher table and a counter.
If I check my tables there is no entry but it does not accept my UNIQUE Key.
Anybody with some ideas?
My test SQL Code for inserting into the view (same result with table)
INSERT INTO V_BRESZ(BRES_ID_LINKKEY, Maskenkey)
VALUES(29, 60);
For example my error message is:
violation of PRIMARY or UNIQUE KEY constraint "UK_BRESZ" on table
"BRESZ". Problematic key value is ("MASKENKEY" = ' 60',
"BRES_ID_LINKKEY" = 29).
But there is no such entry in my table / view.

Cannot insert duplicate key row in object with unique index in SQL Server

Cannot insert duplicate key row in object 'dbo.OCRD' with unique index 'OCRD_ABS_ENTRY'. The duplicate key value is (1). The statement has been terminated.;
An error occurred while updating the entries. See the inner exception for details.; An error occurred while updating the entries. See the inner exception for details.
Inner Exception: Cannot insert duplicate key row in object 'dbo.OCRD' with unique index 'OCRD_ABS_ENTRY'. The duplicate key value is (1). The statement has
been terminated.
An error occurred while updating the entries. See the inner exception for details. An error occurred while updating the entries. See the inner exception for details.
It's an insert on DB
You have a column named "OCRD_ABS_ENTRY" which has an integrity
constraint (unique key or primary key).
You try to insert another entry with value "1" for OCRD_ABS_ENTRY
column, which should be unique.
Try to delete the constraint if it's not necessary, or to insert with
another value.
follow what #Florian already said.
Additional information
insert values into tables with out mentioning value to primary field

Foreign Key Constraint in SQL Server

I have to insert a person's information into a database that is running through SQL Server, and I keep getting a foreign key constraint violation.
I am trying to insert the information into a table called Person.Person, but it says the problem occurred in a table called Person.BusinessEntityID. The BusinessEntityID column is in both tables, so do I have to update both tables at the same time for it to work?
Also I noticed the BusinessEntityID value corresponds with the row number in the database, so would it be possible to set a value equal to the row number in the table?
Thank you!

INSERT statement conflicted with COLUMN FOREIGN KEY

I have a FK in my table that looks like this:
Clearly, I should be able to insert NULL into this column as well as any value that exists in the parent table.
But when I try to insert NULL, I get the following error:
The INSERT statement conflicted with the FOREIGN KEY constraint
"users_fk". The conflict occurred in database "mydatabase", table
"dbo.country", column 'country_id'.
You are absolutely right - you should be able to insert NULL into this column.
Are you 500% sure that you are? The error message says something else - it appears as if you're inserting an invalid value.
Could it be that you have
(a) another line in your script that inserts additional (invalid) values?
Or
(b) do you happen to have a trigger on this table that does something invalid?
Update: (based on your comment)
If you're not specifying the column in the INSERT statement - then maybe an invalid default value is defined on that column? Something like a default of 0 or something.
If you do have a default value - that default value will be inserted (and not NULL, as you seem to be expecting)