Unix Technical Forum

help with update

This is a discussion on help with update within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi all.....Im ok with sql but by no means a guru i have like 100,000 rows of lineitems example: ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:48 PM
Analizer1
 
Posts: n/a
Default help with update

Hi all.....Im ok with sql but by no means a guru

i have like 100,000 rows of lineitems
example:
invoiceid, lineitemId, squencenum
9999999 11111111 1
9999999 11111181 2
9999999 11111182 3

the Data consistes of Many Invoices and the Lineitem id
is a Unique Id (Most are consequtive but not a Guarantee) and SequenceNum
is 1,2,3, etc of the line items on the invoice

In my Case i have say 100,000 rows , different Invoice numbers with
different number of lineitems
All the LineItems are out of Sync.....

So I need to Update them in the order 1,2,3 etc for there prospective
Invoice number

Help with a Update Scripts would be helpful
Thanks


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:48 PM
Plamen Ratchev
 
Posts: n/a
Default Re: help with update

If I understand correctly that the requirement is to update the line item
for be a consecutive number based on invoice and sequence number for line
items, then something like this will do (SQL Server 2005):

CREATE TABLE Invoices (
invoiceid INT NOT NULL,
sequencenum INT NOT NULL,
lineitemid INT NOT NULL UNIQUE,
PRIMARY KEY (invoiceid, sequencenum));

INSERT INTO Invoices VALUES (9999999, 1, 11111111);
INSERT INTO Invoices VALUES (9999999, 2, 11111181);
INSERT INTO Invoices VALUES (9999999, 3, 11111191);
INSERT INTO Invoices VALUES (9999999, 4, 11111171);
INSERT INTO Invoices VALUES (9999998, 1, 11111161);
INSERT INTO Invoices VALUES (9999998, 2, 11111121);
INSERT INTO Invoices VALUES (9999998, 3, 11111131);
INSERT INTO Invoices VALUES (9999997, 1, 11111141);
INSERT INTO Invoices VALUES (9999997, 2, 11111101);
INSERT INTO Invoices VALUES (9999997, 3, 11111151);

WITH InvoicesCTE
AS
( SELECT lineitemid,
ROW_NUMBER() OVER(
ORDER BY invoiceid, sequencenum)
AS line_nbr
FROM Invoices)
UPDATE InvoicesCTE
SET lineitemid = line_nbr;

SELECT invoiceid, sequencenum, lineitemid
FROM Invoices;

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 03:48 PM
--CELKO--
 
Posts: n/a
Default Re: help with update

What you posted makes no sense in an RDBMS.

1) RTables have no ordering; that is a property of a file.

2) ".. SequenceNum is 1,2,3, etc of the line items on the invoice <<

Unh?? Display and formatting is done in the front end and not in the
database. You are still thinking of a COBOL program where display and
data were mixed together in a procedural program, working on one
record at a time

3) "I have say 100,000 rows, different Invoice numbers with different
number of lineitems. All the LineItems are out of synch .. <<

Since a table has no ordering, how can it be "out of synch" --
whatever that means.

4) "So I need to Update them in the order 1,2,3 etc for their
respective Invoice number"

Unh? SQL is a set-oriented language. Updates are done in whole sets
and not row-at-a-time. You are confusing tables with magnetic tape
files. The usual patter is like this skeleton:

CREATE TABLE Invoices
(invoice_nbr INTEGER NOT NULL PRIMARY KEY,
..);

(CREATE TABLE InvoiceDetails
(invoice_nbr INTEGER NOT NULL
REFERENCES Invoices (invoice_nbr),
sku CHAR(10) NOT NULL
REFERENCES Inventory (sku),
PRIMARY KEY (invoice_nbr, sku),
order_qty INTEGER NOT NULL
CHECK(order_qty > 0),
..);

There is no mention of the paper (or video) order form lines in the
RDBMS. They are physical and the RDBMS is logical. You are really
missing basic concepts and need to get help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 03:48 PM
Ed Murphy
 
Posts: n/a
Default Re: help with update

--CELKO-- wrote:

> 4) "So I need to Update them in the order 1,2,3 etc for their
> respective Invoice number"
>
> Unh? SQL is a set-oriented language. Updates are done in whole sets
> and not row-at-a-time. You are confusing tables with magnetic tape
> files. The usual patter is like this skeleton:
>
> CREATE TABLE Invoices
> (invoice_nbr INTEGER NOT NULL PRIMARY KEY,
> ..);
>
> (CREATE TABLE InvoiceDetails
> (invoice_nbr INTEGER NOT NULL
> REFERENCES Invoices (invoice_nbr),
> sku CHAR(10) NOT NULL
> REFERENCES Inventory (sku),
> PRIMARY KEY (invoice_nbr, sku),
> order_qty INTEGER NOT NULL
> CHECK(order_qty > 0),
> ..);
>
> There is no mention of the paper (or video) order form lines in the
> RDBMS. They are physical and the RDBMS is logical. You are really
> missing basic concepts and need to get help.


Many reasonable real-world systems include some type of sort criteria
as well, e.g. OrderDetails may wish to record which line item the
customer ordered first, second, etc. - but of course there's plenty of
room for debate over how best to implement the concept.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:02 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com