Solved

SQL Update lines in PK

Posted on 2014-04-22
4
287 Views
Last Modified: 2014-04-22
In my table the PK is ORDER_NUMBER,LINE_NUMBER. I have routine that will insert a line in between other lines. So if the user wants to insert a new line 10 the below will run. I am curious as to why this works.
update ORDLINES
set LINE_NUMBER = LINE_NUMBER + 1
where ORDER_NUMBER=1000  and LINE_NUMBER >= 10

How does SQL number to go to the last line and increment that number and then work backwards? If it did not do that then there would be a PK duplicate record violation. Is there a better more proper way to accomplish this? In the sp I pass down the value of order and line numbers.
0
Comment
Question by:rwheeler23
[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
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
Dulton earned 500 total points
ID: 40015895
How does SQL number to go to the last line and increment that number and then work backwards? If it did not do that then there would be a PK duplicate record violation. Is there a better more proper way to accomplish this? In the sp I pass down the value of order and line numbers.


Looks good to me.

There isn't a duplicate PK record violation because a couple things:
1. you enter this SP with no duplicates, as constrained by your PK of Order_Number,Line_number.
2. you're incrementing all Line numbers 1, for all records which are line_number >= 10

this means that Order_number 1 has line numbers
1, 4, 7, 9,10,11,12,13,14

when you add 1 to every one over or equal to 10 you get:
1, 4, 7, 9,11,12,13,14,15



-Now, if you would subtract 1 rather than add 1 -or- add 1 where line_number is <= 10, you may very well throw PK constraint errors.
0
 

Author Comment

by:rwheeler23
ID: 40016333
Interesting. I would think it would try to update line 10 to line 11 and then bump into a duplicate. So it looks like it updates 15 to 16, 14 to 15, 13 to 14, 12 to 13, 11 to 12 and finally 10 to 11 leaving 10 available which I create at the very end.
0
 
LVL 6

Expert Comment

by:Dulton
ID: 40016402
Yeah, as long as it's all contained within the same DML statement and you do your insert afterward, the way you're going about it, I don't see any primary key violations.
0
 

Author Closing Comment

by:rwheeler23
ID: 40016450
Thanks for the insight!
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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