Update row from row in same table

Posted on 2007-10-16
Last Modified: 2011-10-03
SQL is not my strong point, but I am familar with it.

I am creating a stored procedure to get the following from a table
select columns where condition is met.  - This works fine

What I need to do now is get the values from the select and update rows in the same table with the values where conditions are met.

Just not sure how to form this.  I know how to do a basic update statement, but not how to pass in variables from select.  Any help would be appreciated.
Question by:bgrantham
    LVL 16

    Expert Comment

    update table
    set column = 'data'
    where condition is met
    LVL 25

    Accepted Solution

    1) Passing true variables
    declare @var1 as int
    declare @var2 as int
    select @var1 = col1,@var2 = col2
    from table where  condition is met

    update table set col3 =@var1,col3=@var2
    where newcond is met

    2) direcly
    In this one the table mentioned after the FROM is the one you want to update, but it needed to be repeated in order to be able to join

    update table  set col3 =t2.col1,col3=t2.col2
    from table as t1
    inner join table as t2 on join-condition met
    where t1.condition met
    and t2 condition met


    Author Comment

    Thanks jogos.  I got it working with your option 1 which is kind of what I was thinking.  I like your option 2 as well and may do something like a little later.

    Thanks again and enjoy the points.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    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…
    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.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    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…

    761 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

    14 Experts available now in Live!

    Get 1:1 Help Now