Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP SQL Syntax Duplicate Key

Posted on 2016-11-07
7
Medium Priority
?
142 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 14

Assisted Solution

by:Nakul Vachhrajani
Nakul Vachhrajani earned 200 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 35

Accepted Solution

by:
Pawan Kumar earned 1800 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

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 14

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

824 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