Complex Update Query

I need to write an update query that will do the following:

Turn this table:
ID     PersonNo     Notes
--     -----------     ------
1       <NULL>      Record: 1
2       <NULL>     <NULL>
3       <NULL>      Note 1
4       <NULL>      Note 2
5       <NULL>      Record: 2
6       <NULL>     <NULL>
7       <NULL>      Note 1
8       <NULL>      Record: 3
9       <NULL>     <NULL>
10     <NULL>      Note 1

Into this table:
ID     PersonNo     Notes
--     -----------     ------
1            1           Record: 1
2            1           <NULL>
3            1           Note 1
4            1           Note 2
5            2           Record: 2
6            2           <NULL>
7            2           Note 1
8            3           Record: 3
9            3           <NULL>
10          3           Note 1
11          3           Note 2
12          3           Note 3
Thanks in advance for your help.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Although I don't know the whole situation, I don't see why you need to update and double-store data when you could create a view with PersonNo as a calculated field, like so:

CREATE VIEW dbo.yourview
  WHEN t1.Notes LIKE '%Record%'
  THEN CONVERT(int,RIGHT(t1.Notes,1))
  ELSE (
   SELECT TOP 1 CONVERT(int,RIGHT(t2.Notes,1))
   FROM dbo.yourtable t2
   WHERE t2.ID<t1.ID AND t2.Notes LIKE '%Record%'
  END AS [PersonNo], t1.Notes
FROM dbo.yourtable t1

Hi rikroket

declare @a table (Id int, PersonNo int, Notes varchar(50))

insert into @a values ( 1 , NULL, 'Record: 1')
insert into @a values ( 2 , NULL   , NULL  )
insert into @a values ( 3 , NULL   , 'Note 1'  )
insert into @a values ( 4 , NULL  ,  'Note 2' )
insert into @a values ( 5 , NULL   , 'Record: 2'  )
insert into @a values ( 6 , NULL  , NULL  )
insert into @a values ( 7 , NULL   , 'Note 1'  )
insert into @a values ( 8 , NULL  , 'Record: 3'  )
insert into @a values ( 9 , NULL  ,  NULL )
insert into @a values ( 10 , NULL  , 'Note 1'  )

update @a
    set PersonNo =
    cast (Replace((select Notes From @a a2 where a2.Id =
        (select max(id) from @a a1 where a1.Id <= a.Id and Notes like 'Record:%')),'Record:','') as int)
from @a a

select * from @a

Cheers Sash
Replace the @a variable with the name of your table
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

update [Table Name]
    set PersonNo =
    cast (Replace((select Notes From [Table Name] a2 where a2.Id =
        (select max(id) from [Table Name] a1 where a1.Id <= a.Id and Notes like 'Record:%')),'Record:','') as int)
from [Table Name] a

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Or, even faster:

update #a       set PersonNo = (select count(*) from #a c where c.ID< and Notes like 'Record:%')

Your Logic only works while all record numbers exist in the table, if a record gets deleted it fails.  
You're right.
rikroketAuthor Commented:
Thanks to all who replied with solutions and thanks to SashP; I implemented your solution and it worked perfectly.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.