Solved

SQL Insert Lines In Between

Posted on 2015-01-09
6
106 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

In this article I will describe the Detach & Attach 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.
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

828 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