Solved

Update table without using a cursor

Posted on 2012-03-22
4
225 Views
Last Modified: 2012-03-23
Hi,

I have a table that looks something like this:

Id                   Col1             col2
1                    H123            Sooners  
2                    H222            Buckeyes
3                    (null)             (null)
4                    (null)             (null)
5                    H234            Gators
6                    (null)            (null)
7                    H566           Ducks
8                    H578           Longhorns


is there a way to replace the null values with values from the previous row that is not null without having to resort to cursors so that the table ends up something like this:

Id                   Col1            col2
1                    H123            Sooners  
2                    H222            Buckeyes
3                    H222            Buckeyes
4                    H222            Buckeyes
5                    H234            Gators
6                    H234            Gators
7                    H566            Ducks
8                    H578            Longhorns
0
Comment
Question by:yechan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
ID: 37752176
Here is my crack at it.  I'm interested to see if there are other responses.

See attachment for testing code that will run in Management Studio


--hold number of nulls
declare @myNulls smallint

--inital number of null values
select      @myNulls = COUNT(*)
from      @myTable
where      Col2 is null

--update from previous row until NULL values are gone
while isnull(@myNulls,0) > 0
begin

      update @myTable
      set            
            Col2 = isnull(a.Col2, (select Col2 from @myTable b where b.Id = (a.Id - 1)))
      ,      Col1 = isnull(a.Col1, (select Col1 from @myTable b where b.Id = (a.Id - 1)))

      from @myTable a

      --get count of remamining nulls
      select @myNulls = COUNT(*)
      from      @myTable
      where      Col2 is null

end

select      *
from      @myTable
update-nulls.sql
0
 
LVL 1

Author Comment

by:yechan
ID: 37752190
This is really great.  Thanks.  I am going to leave this question open for a little while longer per your suggestion as to see what others come up with.

again.  thanks.
0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 37752196
Yes, leave it open, I'm interested to see if there is a solution that can pull this off without using a while loop.  I'm thinking that there probably is.
0
 
LVL 1

Author Comment

by:yechan
ID: 37756578
gohord, I am going ahead and close this ticket.  I have implemented your solution and it works great.

ty.
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach 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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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