Solved

SQL Update lines in PK

Posted on 2014-04-22
4
282 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
  • 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

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 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