Solved

ASP SQL Syntax Duplicate Key

Posted on 2016-11-07
7
115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 13

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 28

Accepted Solution

by:
Pawan Kumar 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 13

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 13

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

756 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