Duplicate Error when inserting unique non duplicate values T-SQL - sql-server

Here is my other mistake I can't fight:
I am trying to add (INSERT INTO sql statement) a unique records from the WHID field
(ClientEpisode table) into WHID field (EHREpisode table):
INSERT INTO
[WH].[Fact].EHREpisode ([WHID])
SELECT
[HP].[bhcms5].ClientEpisode.WHID
FROM
[HP].[bhcms5].ClientEpisode
Both of the WHID fields are unique (non duplicated) in both tables, and between each others, but I keep having an error:
Plz, see the error message, in bigger letters:
"Cannot insert duplicate key row in object 'Fact.EHREpisode' with unique index 'IX_EHREpisode'. The duplicate key value is (NULL, NULL>).
The statement has been terminated."
Below are my tables structures:
EHREpisode:
ClientEpisode:

The error message seems to be saying something about NULL values being the culprit (your screen capture is hard to read). So, you may try excluding NULL values from being inserted:
INSERT INTO [WH].[Fact].EHREpisode ([WHID])
SELECT [HP].[bhcms5].ClientEpisode.WHID
FROM [HP].[bhcms5].ClientEpisode
WHERE [HP].[bhcms5].ClientEpisode.WHID IS NOT NULL;
As an alternative, consider using an index on WHID which ignores NULL values:
CREATE UNIQUE NONCLUSTERED INDEX idx_col1
ON [HP].[bhcms5].ClientEpisode (WHID)
WHERE WHID IS NOT NULL;

The error is very clear: There is a unique index called IX_EHREpisode with such columns that your query tries to insert the value (null,null) two times.
You have provided us a set of SSMS GUI pictures that are not really relevant to this. Instead, the way to use the GUI would be:
Go to the "Object explorer" at the left of SSMS
Find the table "EHREpisode" you are interested in
Open "Indexes". You should find IX_EHREpisode here. Open it.
Inside you will see the columns included in this index. There will be a couple of these that would both take NULL value if your query executed
Thus, you will either have to modify the index, or re-think your query.

Related

Insert records from temp table where record is not already preset fails

I am trying to insert from temporary table into regular one but since there is data in temp table sharing the same values for a primary key of the table I am inserting to, it fails with primary key constraint being violated. That is expected so I am working around it by inserting only the rows that have the primary key not already present in table I am inserting to.
I tried both EXISTS and NOT IN approach, I checked examples showcasing both, confirmed both works in SQL server 2014 in general, yet I am still getting the following error:
Violation of PRIMARY KEY constraint 'PK_dbo.InsuranceObjects'. Cannot
insert duplicate key in object 'dbo.InsuranceObjects'. The duplicate
key value is (3835fd7c-53b7-4127-b013-59323ea35375).
Here is the SQL in NOT IN variance I tried:
print 'insert into InsuranceObjects'
INSERT INTO $(destinDB).InsuranceObjects
(
Id, Value, DefInsuranceObjectId
)
SELECT Id, InsuranceObjectsValue, DefInsuranceObjectId
FROM #vehicle v
WHERE v.Id NOT IN (SELECT Id FROM $(destinDB).InsuranceObjects) -- prevent error when running scrypt multiple times over
GO
If not apparent:
Id is the primary key in question.
$(destinDB) is command line variable. Different from TSQL variable.
It allows me to define the target database and instance in convenient
script based level or even multiple scripts based level. Its used in
multiple variations throughout the code and has so far performed
perfectly. The only downside is you have to run in CMD mode.
when creating all temp tables USE $(some database) is also used so
it's not an issue
I must be missing something completely obvious but it's driving me nuts that such a simple query fails. What is worse, when I try running select without insert part, it returns ALL the records from temp table despite me having confirmed there are duplicates that should fail the NOT IN part in where clause.
I suspect the issue is that you have duplicate ID values in your temp table. Please check the values there as it would cause the issue you are seeing.

T-SQL trouble with conditional select into

I'm running into some problems with my attempts to create a specific table. I have read some similar answers on this site already, but I'm not sure I'm understanding their answers and/or how it applies to my issue.
I have an existing table which contains ingredients for a recipe (table definition below):
Create Table Recipes.dbo.IngredientsByRecipeID
(
RecipeID int,
IngredientName varchar(max),
UnitTypeOfIngredient varchar(max),
QuantityOfIngredient decimal(6,3)
);
I'm trying to create and display the contents of a table which contains all ingredient names used in all recipes, in alphabetical order, and with all duplicate entries removed.
I've tried a few different variations on how to implement it, but I'm not sure now if I'm running into problems with my implementation or problems with how SQL Server works (I'm new to T-SQL and its ways).
My method for sorting the ingredients alphabetically into a new, duplicates-free table is as follows:
/*these three lines represent code which I've been trying to use to control the problems I keep getting with tables existing or not existing, I don't know if they are really helping or not. The first two only drop and create the table, the second is one attempt at a working solution for my needs*/
--drop table AlphabeticallySortedIngredientsList;
--create table AlphabeticallySortedIngredientsList (IngredientName varchar(max));
--select IngredientName into AlphabeticallySortedIngredientsList from IngredientsByRecipeID where ((select count(IngredientName) from AlphabeticallySortedIngredientsList) = 0) order by IngredientName asc;
--here is my latest attempt at getting a working solution for my needs
select IngredientName
into AlphabeticallySortedIngredientsList
from IngredientsByRecipeID
where not exists (select *
from AlphabeticallySortedIngredientsList
where IngredientName = AlphabeticallySortedIngredientsList.IngredientName)
order by IngredientName asc;
With each implementation I initially had some kind of syntax errors, but after (apparently) cleaning up those errors, I'm always left with one last error and while the error changes based on input, the same object is causing the errors. If I leave the 'drop table' line from above uncommented then this is the error I get:
Msg 208, Level 16, State 1, Line 5
Invalid object name 'AlphabeticallySortedIngredientsList'.
If I also uncomment the 'create table AlphabeticallySortedIngredientsList' line, then I get this error:
Msg 2714, Level 16, State 6, Line 9
There is already an object named 'AlphabeticallySortedIngredientsList' in the database.
And if I drop the table and then comment both lines out, I get the same error as above:
Msg 2714, Level 16, State 6, Line 9
There is already an object named 'AlphabeticallySortedIngredientsList' in the database.
I'm perplexed as it seems that my 'select IngredientName into AlphabeticallySortedIngredientList' line cannot make up its mind as to whether or not it is creating the table. If I drop the table before my solution runs, then it says it cannot be found. If I create the table before my solution runs, then it says the table already exists.
I'm guessing the problem is that the 'select' part of the statement is creating AlphabeticallySortedIngredientsList (given it doesn't exist) but then the conditional tries to look for a value in AlphabeticallySortedIngredientsList, which does not exist yet as the operation to create it hasn't occurred yet. Am I on the right track with this assessment, and if so, how can I fix the problem?
I need to preserve the original contents of IngredientsByRecipeID, for the record. AlphabeticallySortedIngredientList only serves the purpose of presenting a clean and orderly list of all ingredients from IngredientsByRecipeID.
I should also mention that if I remove the conditional (as in, I comment out the 'where' clause), then the select into works without any errors. This is with both drop table and create table commented out, and with no table AlphabeticallySortedIngredientList already existing in the database.
Maybe it would be better to just add them all to the table without deleting duplicates, and then go through and delete duplicates? My problem might be with trying to do it all in one step.
The SELECT/INTO syntax creates a new table. You cannot use this and reference the table you are creating in the WHERE clause. Want you want to do is use DISTINCT on the SELECT clause to remove duplicates before you create the table. If you need to use an existing table then you want to use an INSERT statement where the values to insert come from a SELECT clause. Note, if you need to keep the table order by name, it should have the name as the primary key. Since you want unique entries on this, you probably want this anyway.
SELECT DISTINCT IngredientName
INTO AlphabeticallySortedIngredientsList
FROM IngredientsByRecipeID
ADD CONSTRAINT PK_AlphabeticallySortedIngredientsList PRIMARY KEY CLUSTERED (IncredientName)
Generally, I would not suggest keeping this table as you can always get the data from the existing table. Keeping the data in separate tables will force you to periodically update one or the other to keep them in sync. If you do opt to keep the names in a separate table, then you should probably add an Id column, then update the other (original) table with the Id of the name and remove the name column from it. Add a foreign key constraint on the name Id column so that you are required to add the name there and link it to the recipe.
Create table Ingredients
(ingredient varChar(50) primary key not null)
Insert Ingredients(Ingredient)
Select distinct IngredientName
From IngredientsByRecipeID
Yes you are on the right track. The SELECT ... INTO statement will create the table based on the SELECT expression. Trying to access it prematurely in the WHERE NOT EXISTS clause is causing the conflict.
Trying using a SELECT DISTINCT or GROUP BY clause to remove duplicates without relying on WHERE NOT EXISTS.

SQL Server : copy the whole column to another column error

I am trying to move all the data from fælge column in the test table into the fælgen column in table test to with this query
INSERT INTO [dbo].[test2] ([Fælgen])
SELECT Fælge
FROM [dbo].[test];
but I am getting a error with it saying that it cant insert the null into column ET which is not that column i am trying to insert my data to
Msg 515, Level 16, State 2, Line 2
Cannot insert the value NULL into column 'ET', table 'OminiData.dbo.test2'; column does not allow nulls. INSERT fails.
While you are going to Insert data to table you need to make sure that all the constraints are passed and nothing violate them.
You need to make sure that all Non-Null columns have value while inserting the new record. In fact you can not Insert value to single column in a table without considering the constraints and providing value for non-null-able columns.
Finally to overcome this error you have two choice:
Update the destination column(If there is a one-one or one-many relation between Test and Test2, This will need using Update based on Join, other wise a simple Update will be the solution)
Provide non-null value for ET
INSERT INTO [dbo].[test2] ([Fælgen], [ET])
SELECT Fælge , '' as ET
FROM [dbo].[test];
Press right mouse button on the table, chose Script table as... -> INSERT. There will be a script template for insertion. All columns mentioned in this script should have values determined by there datatypes.
I was working on same error . found this from this link SQL Server: copy data from one column to another column?
Using Merge I was able to copy data from one column to another in two different table.
Make sure data type for both columns need to get copied, and row count for both tables.
You cannot insert values to a single DB columns. You always insert entire rows. So your statement doesn't really mean insert some values to this column. What it really means is insert some values to this column and null/default values to all other columns. If any of those other columns does not allow nulls and doesn't have a non-null default, insert will fail.

Is it possible to determine what caused a duplicate key values

I have a job who makes replication between two databases and after I insert new value in first table tbl_company_market_service_phone_number I get this error from the job ,and the value not exists in table wich is replicated data (second table).
Error:Cannot insert duplicate key row in object 'tbl_company_market_service_phone_number' with
unique index 'IX_tbl_company_market_service_phone_number_fld_uk'. The duplicate key value is
(65b763ac-6f8f-4fe6-b76c-02a75b71dbe1).
How can I find what key value is (65b763ac-6f8f-4fe6-b76c-02a75b71dbe1) ? Or find out the cause of job faill.
So it is not in the replicated data
Did you check the source table
It is telling you table name tbl_company_market_service_phone_number
And that is the name of stated name of the source table

Detailed error message for violation of Primary Key constraint in sql2008?

I'm inserting a large amount of rows into an empty table with a primary key constraint on one column.
If there is a duplicate key error, is there any way to find out the value of the key (or row) that caused the error?
Validating the data prior to the insert is sadly not something I can do right now.
Using SQL 2008.
Thanks!
Doing the count(*) / group by thing is something I'm trying to avoid, this is an insert of hundreds of millions of rows from hundreds of different DB's (some of which are on remote servers)...I don't have the time or space to do the insert twice.
The data is supposed to be unique from the providers, but unfortunately their validation doesn't seem to work correctly 100% of the time and I'm trying to at least see where it's failing so I can help them troubleshoot.
Thank you!
There's not a way of doing it that won't slow your process down, but here's one way that will make it easier. You can add an instead-of trigger on that table for inserts and updates. The trigger will check each record before inserting it and make sure it won't cause a primary key violation. You can even create a second table to catch violations, and have a different primary key (like an identity field) on that one, and the trigger will insert the rows into your error-catching table.
Here's an example of how the trigger can work:
CREATE TRIGGER mytrigger ON sometable
INSTEAD OF INSERT
AS BEGIN
INSERT INTO sometable SELECT * FROM inserted WHERE ISNUMERIC(somefield) = 1 FROM inserted;
INSERT INTO sometableRejects SELECT * FROM inserted WHERE ISNUMERIC(somefield) = 0 FROM inserted;
END
In that example, I'm checking a field to make sure it's numeric before I insert the data into the table. You'll need to modify that code to check for primary key violations instead - for example, you might join the INSERTED table to your own existing table and only insert rows where you don't find a match.
The solution would depend on how often this happens. If it's <10% of the time then I would do the following:
Insert the data
If error then do Bravax's revised solution (remove constraint, insert, find dup, report and kill dup, enable constraint).
This means it's only costing you on the few times an error occurs.
If this is happening more often then I'd look at sending the boys over to see the providers :-)
Revised:
Since you don't want to insert twice, could you:
Drop the primary key constraint.
Insert all data into the table
Find any duplicates, and remove them
Then re-add the primary key constraint
Previous reply:
Insert the data into a duplicate of the table without the primary key constraint.
Then run a query on it to determine rows which have duplicate values for the rpimary key column.
select count(*), <Primary Key>
from table
group by <Primary Key>
having count(*) > 1
Use SSIS to import the data and have it check for this as part of the data flow. That is the best way to handle. SSIS can send the bad records to a table (that you can later send to the vendor to help them clean up their act) and process the good ones.
I can't believe that SSIS does not easily address this "reality", because, let's face it, oftentimes you need and want to be able to:
See if a record exists with a certain unique or primary key
If it does not, insert it
If it does, either ignore it or update it.
I don't understand how they would let a product out the door without this capability built-in in an easy-to-use manner. Like, say, set an attribute of a component to automatically check this.

Resources