Solved

SQL Insert Lines In Between

Posted on 2015-01-09
6
108 Views
Last Modified: 2015-01-09
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
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
  • 3
  • 3
6 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40541014
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
 

Author Comment

by:rwheeler23
ID: 40541318
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
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40541336
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:rwheeler23
ID: 40541458
That is the key comment. As long as the constraint is not checked until after the upgrade your suggestion will work.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40541465
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
 

Author Closing Comment

by:rwheeler23
ID: 40541527
Thanks for the tips
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

733 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