Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP SQL Syntax Duplicate Key

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

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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

722 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