June 8, 2016 at 9:20 am
I am getting a Primary Key Violation when Loading a Table.
I queried the source table and there is only one record with value of the primary key.
This does not make sense.
Any ideas?
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
June 8, 2016 at 9:22 am
Welsh Corgi (6/8/2016)
I am getting a Primary Key Violation when Loading a Table.I queried the source table and there is only one record with value of the primary key.
This does not make sense.
Any ideas?
check your source; you could have two of the same value in your temp/staging table before it is being loaded into your Target table; you might need to filter your source prior to insert.
are you joining your staging to target to eliminate inserting duplicates?
Lowell
June 8, 2016 at 9:44 am
Thanks. I checked it that does ot apply.
Select tblNexus.* from TblNexus WITH (NOLOCK)
Inner Join PrismClients WITH (NOLOCK) on TblNexus.PrismclientID = TblNexus.PrismClientID and PrismClients.CountryID = 2
It just happened with another table, a simple lookup table.
I do this all of the time and I do not have any problems.
I tried the import wizard and it is not showing me the source table to select from.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
June 8, 2016 at 9:52 am
I added a distinct on the lookup table and I did not get duplicates.
SELECT DISTINCT Catevent.*
FROM CatEvent
Inner join CatEventArea on CatEventArea.CatEventID = CatEvent.CatEventID and CATEventArea.CountryID =
Thank you Sir.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
June 8, 2016 at 9:55 am
Welsh Corgi (6/8/2016)
I am getting a Primary Key Violation when Loading a Table.I queried the source table and there is only one record with value of the primary key.
In that case, there must be one row in the source table with that PK value, and one row already in the destination table.
John
June 8, 2016 at 10:45 am
John Mitchell-245523 (6/8/2016)
Welsh Corgi (6/8/2016)
I am getting a Primary Key Violation when Loading a Table.I queried the source table and there is only one record with value of the primary key.
In that case, there must be one row in the source table with that PK value, and one row already in the destination table.
John
I'm truncating the tables prior to the load.
My source query was the offender.
Thanks.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
June 8, 2016 at 11:07 am
Welsh Corgi (6/8/2016)
John Mitchell-245523 (6/8/2016)
Welsh Corgi (6/8/2016)
I am getting a Primary Key Violation when Loading a Table.I queried the source table and there is only one record with value of the primary key.
In that case, there must be one row in the source table with that PK value, and one row already in the destination table.
John
I'm truncating the tables prior to the load.
My source query was the offender.
Thanks.
Just for clarity, that is exactly what Lowell told you to check.
June 8, 2016 at 11:08 am
I execute the following query:
SELECT DISTINCT ID, Name, Abbreviation, CountryID
FROM Postal_StateProvince
WHERE CountryID = 2
It returns the following values:
IDNameAbbreviationCountryID
52AlbertaAB2
53British ColumbiaBC2
54ManitobaMB2
55New BrunswickNB2
56NewfoundlandNL2
57Northwest TerritoriesNT2
58Nova ScotiaNS2
59NunavutNU2
60OntarioON2
61Prince Edward IslandPE2
62QuebecQC2
63SaskatchewanSK2
64Yukon TerritoryYT2
It return the following error:
"Cannot insert the value NULL into column 'Abbreviation', table 'PrismDataCanada.dbo.Postal_StateProvince'; column does not allow nulls. INSERT fails.".
I create an import wizard and I do not get an error.
That is weird?
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
June 8, 2016 at 11:10 am
import wizard is probably dropping and recreating the tables;
the existing table says NOT NULL,
but whatever the wizard creates allows nulls.
pretty easy to check, compare the table definitions.
Lowell
June 8, 2016 at 11:12 am
Welsh Corgi (6/8/2016)
I execute the following query:
SELECT DISTINCT ID, Name, Abbreviation, CountryID
FROM Postal_StateProvince
WHERE CountryID = 2
It returns the following values:
IDNameAbbreviationCountryID
52AlbertaAB2
53British ColumbiaBC2
54ManitobaMB2
55New BrunswickNB2
56NewfoundlandNL2
57Northwest TerritoriesNT2
58Nova ScotiaNS2
59NunavutNU2
60OntarioON2
61Prince Edward IslandPE2
62QuebecQC2
63SaskatchewanSK2
64Yukon TerritoryYT2
It return the following error:
"Cannot insert the value NULL into column 'Abbreviation', table 'PrismDataCanada.dbo.Postal_StateProvince'; column does not allow nulls. INSERT fails.".
I create an import wizard and I do not get an error.
That is weird?
What is returning an error? I see nothing inserting anything into anything in the quoted post.
June 8, 2016 at 11:24 am
Lowell (6/8/2016)
import wizard is probably dropping and recreating the tables;the existing table says NOT NULL,
but whatever the wizard creates allows nulls.
pretty easy to check, compare the table definitions.
The wizard appends the record to the table.
I save the Import package and added it to my solution.
Thanks.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
June 8, 2016 at 3:49 pm
Welsh Corgi (6/8/2016)
I execute the following query:
SELECT DISTINCT ID, Name, Abbreviation, CountryID
FROM Postal_StateProvince
WHERE CountryID = 2
It returns the following values:
IDNameAbbreviationCountryID
52AlbertaAB2
53British ColumbiaBC2
54ManitobaMB2
55New BrunswickNB2
56NewfoundlandNL2
57Northwest TerritoriesNT2
58Nova ScotiaNS2
59NunavutNU2
60OntarioON2
61Prince Edward IslandPE2
62QuebecQC2
63SaskatchewanSK2
64Yukon TerritoryYT2
It return the following error:
"Cannot insert the value NULL into column 'Abbreviation', table 'PrismDataCanada.dbo.Postal_StateProvince'; column does not allow nulls. INSERT fails.".
I create an import wizard and I do not get an error.
That is weird?
First, that's not a Primary Key violation.
Second, a SELECT cannot return an error about inserts. You've not posted the entire code that you were using so it's not possible to figure out if it's weird or not. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 12 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply