Solved

Update table without using a cursor

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Master DB with Masterkey 1 40
Using this function 4 53
Estimating my database size 7 51
store vs query adhoc - no show rows 4 37
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…

737 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