[Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint












1















I have an inventory database to track some equipment which I sometimes loan out. I have a Device table, and also a DeviceHistory table. There are two forms which I use to update the Device record, and updates are also recorded in the DeviceHistory table. Both forms call the same update function. I am temporarily writing out the sql to try to locate the differences. When I use formA it results in:



insert into QADeviceHistory (DeviceID, Timestamp, StatusID, AuthorID, AssignedToID, History) 
values ( 264, '11/15/2018 9:31:10 AM', 'AVAIL', 'rongray', '', '');


and everything works just fine. However, when I use formB it results in:



insert into QADeviceHistory (DeviceID, Timestamp, StatusID, AuthorID, AssignedToID, History) 
values ( 264, '11/15/2018 9:31:45 AM', 'AVAIL', 'rongray', '', '');



Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY
KEY constraint 'QADeviceHistory_pk'. Cannot insert duplicate key in
object 'dbo.QADeviceHistory'. /py2/DeviceRecord.asp, line 1393




The primary key for DeviceHistory is DeviceID and Timestamp, and the values I am attempting to enter really are unique. Oddly enough, the DeviceHistory record DOES get written to the table, so I really don't understand why I am getting the error when using formB but not getting the error when using formA. I am tempted to just add an on resume next and ignore it but would at least like to understand what's happening.



(Also, this is not new code... it's been around for a few years. The only recent change is that I had to migrate the database from a Win 2008 server to a Win 2016 server, and both servers are using MS SQL Server 2008.)










share|improve this question




















  • 2





    The error is very clear. You tried to insert a record with an existing ID. It doesn't matter at all how old the code is if the new values are duplicate, or if a bug in the code results in invalid data.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:31













  • As for the core working /py2/DeviceRecord.asp, line 1393 is extremely worrying. A 1400+ long VBScript file that performs data access can easily hide errors. VBScript didn't have exceptions making it easy to ignore errors and just keep running. An ingored error could mean that the code tried to add ID 0 twice for example.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:34











  • Another issue is that Classic ASP/VBScript had half-baked Unicode support. BSTR strings were Unicode but ASP required explicit configurationto work with Unicode.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:37











  • In any case, without any code it's not possible to help. If you want to check what the code really does, use SQL Server Profiler or Extended Events to see what SQL queries are actually executed.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:38






  • 1





    Are there any triggers on the table which might be doing something funky?

    – Dave Cullum
    Nov 15 '18 at 15:50
















1















I have an inventory database to track some equipment which I sometimes loan out. I have a Device table, and also a DeviceHistory table. There are two forms which I use to update the Device record, and updates are also recorded in the DeviceHistory table. Both forms call the same update function. I am temporarily writing out the sql to try to locate the differences. When I use formA it results in:



insert into QADeviceHistory (DeviceID, Timestamp, StatusID, AuthorID, AssignedToID, History) 
values ( 264, '11/15/2018 9:31:10 AM', 'AVAIL', 'rongray', '', '');


and everything works just fine. However, when I use formB it results in:



insert into QADeviceHistory (DeviceID, Timestamp, StatusID, AuthorID, AssignedToID, History) 
values ( 264, '11/15/2018 9:31:45 AM', 'AVAIL', 'rongray', '', '');



Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY
KEY constraint 'QADeviceHistory_pk'. Cannot insert duplicate key in
object 'dbo.QADeviceHistory'. /py2/DeviceRecord.asp, line 1393




The primary key for DeviceHistory is DeviceID and Timestamp, and the values I am attempting to enter really are unique. Oddly enough, the DeviceHistory record DOES get written to the table, so I really don't understand why I am getting the error when using formB but not getting the error when using formA. I am tempted to just add an on resume next and ignore it but would at least like to understand what's happening.



(Also, this is not new code... it's been around for a few years. The only recent change is that I had to migrate the database from a Win 2008 server to a Win 2016 server, and both servers are using MS SQL Server 2008.)










share|improve this question




















  • 2





    The error is very clear. You tried to insert a record with an existing ID. It doesn't matter at all how old the code is if the new values are duplicate, or if a bug in the code results in invalid data.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:31













  • As for the core working /py2/DeviceRecord.asp, line 1393 is extremely worrying. A 1400+ long VBScript file that performs data access can easily hide errors. VBScript didn't have exceptions making it easy to ignore errors and just keep running. An ingored error could mean that the code tried to add ID 0 twice for example.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:34











  • Another issue is that Classic ASP/VBScript had half-baked Unicode support. BSTR strings were Unicode but ASP required explicit configurationto work with Unicode.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:37











  • In any case, without any code it's not possible to help. If you want to check what the code really does, use SQL Server Profiler or Extended Events to see what SQL queries are actually executed.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:38






  • 1





    Are there any triggers on the table which might be doing something funky?

    – Dave Cullum
    Nov 15 '18 at 15:50














1












1








1








I have an inventory database to track some equipment which I sometimes loan out. I have a Device table, and also a DeviceHistory table. There are two forms which I use to update the Device record, and updates are also recorded in the DeviceHistory table. Both forms call the same update function. I am temporarily writing out the sql to try to locate the differences. When I use formA it results in:



insert into QADeviceHistory (DeviceID, Timestamp, StatusID, AuthorID, AssignedToID, History) 
values ( 264, '11/15/2018 9:31:10 AM', 'AVAIL', 'rongray', '', '');


and everything works just fine. However, when I use formB it results in:



insert into QADeviceHistory (DeviceID, Timestamp, StatusID, AuthorID, AssignedToID, History) 
values ( 264, '11/15/2018 9:31:45 AM', 'AVAIL', 'rongray', '', '');



Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY
KEY constraint 'QADeviceHistory_pk'. Cannot insert duplicate key in
object 'dbo.QADeviceHistory'. /py2/DeviceRecord.asp, line 1393




The primary key for DeviceHistory is DeviceID and Timestamp, and the values I am attempting to enter really are unique. Oddly enough, the DeviceHistory record DOES get written to the table, so I really don't understand why I am getting the error when using formB but not getting the error when using formA. I am tempted to just add an on resume next and ignore it but would at least like to understand what's happening.



(Also, this is not new code... it's been around for a few years. The only recent change is that I had to migrate the database from a Win 2008 server to a Win 2016 server, and both servers are using MS SQL Server 2008.)










share|improve this question
















I have an inventory database to track some equipment which I sometimes loan out. I have a Device table, and also a DeviceHistory table. There are two forms which I use to update the Device record, and updates are also recorded in the DeviceHistory table. Both forms call the same update function. I am temporarily writing out the sql to try to locate the differences. When I use formA it results in:



insert into QADeviceHistory (DeviceID, Timestamp, StatusID, AuthorID, AssignedToID, History) 
values ( 264, '11/15/2018 9:31:10 AM', 'AVAIL', 'rongray', '', '');


and everything works just fine. However, when I use formB it results in:



insert into QADeviceHistory (DeviceID, Timestamp, StatusID, AuthorID, AssignedToID, History) 
values ( 264, '11/15/2018 9:31:45 AM', 'AVAIL', 'rongray', '', '');



Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY
KEY constraint 'QADeviceHistory_pk'. Cannot insert duplicate key in
object 'dbo.QADeviceHistory'. /py2/DeviceRecord.asp, line 1393




The primary key for DeviceHistory is DeviceID and Timestamp, and the values I am attempting to enter really are unique. Oddly enough, the DeviceHistory record DOES get written to the table, so I really don't understand why I am getting the error when using formB but not getting the error when using formA. I am tempted to just add an on resume next and ignore it but would at least like to understand what's happening.



(Also, this is not new code... it's been around for a few years. The only recent change is that I had to migrate the database from a Win 2008 server to a Win 2016 server, and both servers are using MS SQL Server 2008.)







sql-server primary-key






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:43









Álvaro González

105k30185273




105k30185273










asked Nov 15 '18 at 15:19









RonRon

61




61








  • 2





    The error is very clear. You tried to insert a record with an existing ID. It doesn't matter at all how old the code is if the new values are duplicate, or if a bug in the code results in invalid data.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:31













  • As for the core working /py2/DeviceRecord.asp, line 1393 is extremely worrying. A 1400+ long VBScript file that performs data access can easily hide errors. VBScript didn't have exceptions making it easy to ignore errors and just keep running. An ingored error could mean that the code tried to add ID 0 twice for example.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:34











  • Another issue is that Classic ASP/VBScript had half-baked Unicode support. BSTR strings were Unicode but ASP required explicit configurationto work with Unicode.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:37











  • In any case, without any code it's not possible to help. If you want to check what the code really does, use SQL Server Profiler or Extended Events to see what SQL queries are actually executed.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:38






  • 1





    Are there any triggers on the table which might be doing something funky?

    – Dave Cullum
    Nov 15 '18 at 15:50














  • 2





    The error is very clear. You tried to insert a record with an existing ID. It doesn't matter at all how old the code is if the new values are duplicate, or if a bug in the code results in invalid data.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:31













  • As for the core working /py2/DeviceRecord.asp, line 1393 is extremely worrying. A 1400+ long VBScript file that performs data access can easily hide errors. VBScript didn't have exceptions making it easy to ignore errors and just keep running. An ingored error could mean that the code tried to add ID 0 twice for example.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:34











  • Another issue is that Classic ASP/VBScript had half-baked Unicode support. BSTR strings were Unicode but ASP required explicit configurationto work with Unicode.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:37











  • In any case, without any code it's not possible to help. If you want to check what the code really does, use SQL Server Profiler or Extended Events to see what SQL queries are actually executed.

    – Panagiotis Kanavos
    Nov 15 '18 at 15:38






  • 1





    Are there any triggers on the table which might be doing something funky?

    – Dave Cullum
    Nov 15 '18 at 15:50








2




2





The error is very clear. You tried to insert a record with an existing ID. It doesn't matter at all how old the code is if the new values are duplicate, or if a bug in the code results in invalid data.

– Panagiotis Kanavos
Nov 15 '18 at 15:31







The error is very clear. You tried to insert a record with an existing ID. It doesn't matter at all how old the code is if the new values are duplicate, or if a bug in the code results in invalid data.

– Panagiotis Kanavos
Nov 15 '18 at 15:31















As for the core working /py2/DeviceRecord.asp, line 1393 is extremely worrying. A 1400+ long VBScript file that performs data access can easily hide errors. VBScript didn't have exceptions making it easy to ignore errors and just keep running. An ingored error could mean that the code tried to add ID 0 twice for example.

– Panagiotis Kanavos
Nov 15 '18 at 15:34





As for the core working /py2/DeviceRecord.asp, line 1393 is extremely worrying. A 1400+ long VBScript file that performs data access can easily hide errors. VBScript didn't have exceptions making it easy to ignore errors and just keep running. An ingored error could mean that the code tried to add ID 0 twice for example.

– Panagiotis Kanavos
Nov 15 '18 at 15:34













Another issue is that Classic ASP/VBScript had half-baked Unicode support. BSTR strings were Unicode but ASP required explicit configurationto work with Unicode.

– Panagiotis Kanavos
Nov 15 '18 at 15:37





Another issue is that Classic ASP/VBScript had half-baked Unicode support. BSTR strings were Unicode but ASP required explicit configurationto work with Unicode.

– Panagiotis Kanavos
Nov 15 '18 at 15:37













In any case, without any code it's not possible to help. If you want to check what the code really does, use SQL Server Profiler or Extended Events to see what SQL queries are actually executed.

– Panagiotis Kanavos
Nov 15 '18 at 15:38





In any case, without any code it's not possible to help. If you want to check what the code really does, use SQL Server Profiler or Extended Events to see what SQL queries are actually executed.

– Panagiotis Kanavos
Nov 15 '18 at 15:38




1




1





Are there any triggers on the table which might be doing something funky?

– Dave Cullum
Nov 15 '18 at 15:50





Are there any triggers on the table which might be doing something funky?

– Dave Cullum
Nov 15 '18 at 15:50












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322568%2fmicrosoftodbc-sql-server-driversql-serverviolation-of-primary-key-constrai%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322568%2fmicrosoftodbc-sql-server-driversql-serverviolation-of-primary-key-constrai%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()