Solved

Update table without using a cursor

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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