If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > SQLSTATE = 23000 error

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-09-11, 21:44
furei1976 furei1976 is offline
Registered User
 
Join Date: Jul 2011
Posts: 5
SQLSTATE = 23000 error

Hi Everyone,

I am currently working on a maintenance of a Powerbuilder App using MS SQL.

Here is the scenario, this module uploads one of more text files and checks on duplicate entries. In usual file uploads it will do fine, store the duplicates in the database and to a text file. But in some cases, it just skips the text file encountering "SQLSTATE = 23000 Microsoft OLE DB Provider for SQL Server Violation of PRIMARY KEY constraint".

I was asked to do to make this error go away (if possible) and have the valid and duplicate contents stored in the database as it normally should.

I believe this happens whenever a valid row already existed in the database.
Table_Serials for example, gets data for 'serials' from table_template. table_template has seq_no as unique identifier. but once column 'serials' is inserted to Table_Serials, it will become the primary key. So that is why this error shows up.

I thought about creating a temp table as staging area to separate immediately the duplicates for this matter, then once it is filtered it will do the usual process. But not sure if this will work.

Can someone please suggest a more cost effective solution for this?

Thanks in advance.

furei
Reply With Quote
  #2 (permalink)  
Old 11-10-11, 02:16
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Quote:
I thought about creating a temp table as staging area to separate immediately the duplicates for this matter, then once it is filtered it will do the usual process.
That's the way to go. Don't define PK's in the staging table.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #3 (permalink)  
Old 12-07-11, 01:18
furei1976 furei1976 is offline
Registered User
 
Join Date: Jul 2011
Posts: 5
I already did this but it still stops to the same SQL error code. Is there a way to have the remaining dups inserted to another table automatically if that error shows up?

thanks in advance.

furei
Reply With Quote
  #4 (permalink)  
Old 12-07-11, 02:06
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Write the SQL script to identify the records with the same PK in the staging table compared to the final table (INNER JOIN).
Alter your script, so it inserts them into the "duplicates" table.
Alter your script, so it deletes those from the staging table.
Now copy the records from the staging table to the final table.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #5 (permalink)  
Old 12-07-11, 02:21
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
When the staging table itself may contain duplicate records (with same PK), you could also write a script to identify those records, add them to the "duplicates" table and add DISTINCT to the script that inserts the records from the staging table into the final table.

Don't forget to do a TRUNCATE TABLE StagingTable before each and every batch.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On