Solved

ASP SQL Syntax Duplicate Key

Posted on 2016-11-07
7
120 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Convert rows to columns 5 34
When are cursors useful? 8 62
SQL Server Agent "Access Denied" Error 3 38
SQL query joining 6 tables in asp.net 4 33
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

752 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