[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Update row from row in same table

Posted on 2007-10-16
Medium Priority
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

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

Accepted Solution

jogos earned 500 total points
ID: 20086510
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

ID: 20087603
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.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

872 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