Solved

ASP SQL Syntax Duplicate Key

Posted on 2016-11-07
7
68 Views
Last Modified: 2016-11-07
I have a table tbl_SerialNumbers the Primary key is DispatchNote and Serial. I am using the following to update but get a primary key error "[Microsoft][SQL Server Native Client 10.0][SQL Server]Violation of PRIMARY KEY constraint 'PK_tbl_SerialNumbers_1'. Cannot insert duplicate key in object 'dbo.tbl_SerialNumbers'. The duplicate key value is (000000000601437, 33132132132 )."

This happens even though the values are not already in the table! I have tested typing directly into the table and that works fine.

        SqlStr ="SELECT DispatchNote, Serial, UsrUpdated, StockCode, SalesOrder, Updated FROM dbo.tbl_SerialNumbers "
        SqlStr = SqlStr &  "Where DispatchNote = '" & Dnote & "' And Serial = '" & Seri & "'"   
        Set Rst = dbConnect.Execute(SQLstr)
        If Rst.EOF = true then 
            SqlStr = "Update tbl_SerialNumbers "
            SqlStr = SqlStr & "SET DispatchNote ='" & Dnote & "', Serial = '" & Seri & "', UsrUpdated = '" & Usr & "', SalesOrder = '" & Sord & "', Updated = getdate()"
            Set Rst = dbConnect.Execute(SQLstr)
        end if	

Open in new window


Can anyone help?
0
Comment
Question by:HKFuey
  • 3
  • 3
7 Comments
 
LVL 11

Assisted Solution

by:Nakul Vachhrajani
Nakul Vachhrajani earned 50 total points
ID: 41876944
If you need to update the "SalesOrder" and the "Updated" columns, you don't need to update the primary key fields again :)

The PKey fields are used to uniquely identify a row, and you are doing that via the WHERE clause.

Just remove them from the code, and you should be all set.

SqlStr ="SELECT DispatchNote, Serial, UsrUpdated, StockCode, SalesOrder, Updated FROM dbo.tbl_SerialNumbers "
SqlStr = SqlStr &  "Where DispatchNote = '" & Dnote & "' And Serial = '" & Seri & "'"   
Set Rst = dbConnect.Execute(SQLstr)
If Rst.EOF = true then 
    SqlStr = "Update tbl_SerialNumbers "
    SqlStr = SqlStr & "SET UsrUpdated = '" & Usr & "', SalesOrder = '" & Sord & "', Updated = getdate()"
    Set Rst = dbConnect.Execute(SQLstr)
end if	

Open in new window

0
 

Author Comment

by:HKFuey
ID: 41876946
Hi Nakul,

This is not an 'update' just adding an entirely new row.
0
 
LVL 18

Accepted Solution

by:
Pawan Kumar Khowal earned 450 total points
ID: 41876947
try using this..

IF NOT EXISTS (SELECT TOP 1 1 FROM dbo.tbl_SerialNumbers WHERE Serial  = @Serial  AND DNote =@DNote )
 
BEGIN
     INSERT INTO dbo.tbl_SerialNumbers VALUES ..

END

Open in new window

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 11

Expert Comment

by:Nakul Vachhrajani
ID: 41876952
Ok, let me walk though the code:
1. The code first executes the SELECT using the DispatchNote and Serial value
2. If you don't get a resultset (Rst.EOF = true), then
3. The code is executing an - "Update tbl_SerialNumbers...."

So, first thing would be to change the UPDATE to an INSERT. You need a query like:

If Rst.EOF = true then 
    SqlStr = "INSERT INTO tbl_SerialNumbers (DispatchNote, Serial, UsrUpdated, SalesOrder, Updated) "
    SqlStr = SqlStr & "VALUES ('" & Dnote & ", " & Seri & ", " & Usr & ", " & Sord & ", getdate())"
    Set Rst = dbConnect.Execute(SQLstr)
end if	

Open in new window


Also, can you confirm the datatypes for Rst, dbConnect and SqlStr?
0
 

Author Closing Comment

by:HKFuey
ID: 41876953
Thanks guys, Gave Nakul point as he made me realize where I was going wrong!
0
 
LVL 11

Expert Comment

by:Nakul Vachhrajani
ID: 41876954
Also, the Primary Key violation tells us that the record already exists. So, do check if you have any duplicate code that is also being executed at the same time that goes ahead and inserts the row.

Essentially, you only need to supply the values for the primary key during an INSERT - in all other cases (SELECT/UPDATE/DELETE), these values will be in the WHERE clause.
0
 

Author Comment

by:HKFuey
ID: 41876958
Thanks Nakul, I would give you more points but I already closed the questiomnj ;)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now