Solved

SQL Insert Lines In Between

Posted on 2015-01-09
6
98 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:ScottPletcher
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:
ScottPletcher 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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:ScottPletcher
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Backup & Restore 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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now