Solved

Update table without using a cursor

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Webservices in T-SQL 3 41
Why I am having this error in my query? 2 32
Are triggers slow? 7 22
selective rebuild of SQL Tables in scheduled job 10 37
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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