You are here: Re: help with update « MsSQL Server « IT news, forums, messages
Re: help with update

Posted by Plamen Ratchev on 01/31/08 17:41

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация