• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 124
  • Last Modified:

SQL Insert Lines In Between

What is the proper technique to insert lines into a SQL table using the primary key? As an example, let's say I have I have an order entry table. The PK is ORDRNMBR,LINENMBR. The user types in 20 lines on an order and then realizes he/she forgot to enter line 4. So what you need to do is:
OrderTable.ORDRNMBR,OrderTable.LINENMBR lines 4-20 need to become 5-21 freeing up line 4 so it can be inserted. How do you structure your update statement so it will start at line 20 and come down to line 4? If you were to start at line 4 and update upward you would run into duplicate line numbers.
0
rwheeler23
Asked:
rwheeler23
  • 3
  • 3
1 Solution
 
Scott PletcherSenior DBACommented:
You could number by 10 or 100 instead of 1 so that you can add row(s) inbetween if you need to.  You could use ROW_NUMBER() to display sequential numbers to the user instead of the fragmented ones if you wanted to.
0
 
rwheeler23Author Commented:
That is a good idea for most sane people. However, in our case we have inherited an old program that uses sequential
line numbers. This was due to the fact that the clients demand to see line numbers on quotations and if they do order something it better have the exact same line number on the invoice as appeared on the quotation or they will not pay their invoice. Changing the line sequence is not an option. Is there any way to get the update to proceed from highest to lowest thereby leaving a gap when the update is finished?
0
 
Scott PletcherSenior DBACommented:
You should just be able to UPDATE them directly beginning at that line:

UDPATE OrderTable
SET LINENMBR = LINENMBR + 1
WHERE
    ORDRNMBR = <value> AND
    LINENMBR >= 4

INSERT INTO OrderTable ( ..., LINENMBR )
SELECT ..., 4

I don't think you have to start @ line 20, since I don't SQL doesn't check for the key conflict until after all changes are in place (?!).
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rwheeler23Author Commented:
That is the key comment. As long as the constraint is not checked until after the upgrade your suggestion will work.
0
 
Scott PletcherSenior DBACommented:
Yep: you have to do all the UPDATEs in one, atomic statement, all rows at once, or you will run into dup key issues.

Worst case, you'd have to temporarily disable the constraint, do the UPDATE(s), then re-check that constraint to make it valid again (very important step!).
0
 
rwheeler23Author Commented:
Thanks for the tips
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.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now