Solved

Update table without using a cursor

Posted on 2012-03-22
4
218 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now