Solved

SQL Insert Lines In Between

Posted on 2015-01-09
6
110 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

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

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.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

707 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