I tried your code and got an error?
Msg 4104, Level 16, State 1, Line 14
The multi-part identifier "pc.record_set" could not be bound.
I think my snippet below reflects my query above.
Main Topics
Browse All TopicsI have the table below in SQL Server 2005:
id reference record_id
1 1234-4413 1
2 1234-0d33 1
3 NULL 1
4 1234-0d18 1
5 aaa 1
6 1234-3913 1
7 1234-4223 1
If there is not a one in the record_set field, then that field need's the previous record's reference number.
i.e. record 5 does not have a 1 set, therefore it needs the reference number from 4 and record_id needs to be set to two. Giving me the following, after an update:
id reference record_id
1 1234-4413 1
2 1234-0d33 1
3 1234-0d33 2
4 1234-0d18 1
5 1234-0d18 2
6 1234-3913 1
7 1234-4223 1
I have to update large numbers of records [400'000+], so cursors are not an option.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Ok, assuming the ids ARE sequential, I think this UPDATE will do it:
UPDATE p
SET reference_number = baseP.reference_number,
record_set = CAST(baseP.record_set AS int) + 1
FROM products p
INNER JOIN products baseP ON baseP.id = p.id - 1 AND
baseP.reference_number LIKE '1%'
WHERE p.reference_number IS NULL OR p.reference_number NOT LIKE '1%'
If there could be more than 1 row in a row, then add a loop to execute the code until no "missing" reference_numbers are found:
DECLARE @forceRowcountTo1 TINYINT
SET @forceRowcountTo1 = 1
WHILE @@ROWCOUNT > 0
UPDATE p
SET reference_number = baseP.reference_number,
record_set = CAST(baseP.record_set AS int) + 1
FROM products p
INNER JOIN products baseP ON baseP.id = p.id - 1 AND
baseP.reference_number LIKE '1%'
WHERE p.reference_number IS NULL OR p.reference_number NOT LIKE '1%'
@RiteshShah:
Thanks for your prompt responses, I will test tomorrow.
@ScottPletcher:
I currently have a function which performs something similar to the code you provided, but it's very sloooow. This is not a slight on your code [as I have not analysed it], but I need something lean and mean.
Which method might give me better results on if it were run on 400k+ rows?
Business Accounts
Answer for Membership
by: RiteshShahPosted on 2009-03-31 at 06:11:11ID: 24028478
have a look
Select allOpen in new window