Update a SQL Table if conditions are true.

I need to UPDATE a Oracle DB. I am learning SQL, so please forgive my newbie question.

I have successfully created this Oracle SQL Query

Select person.personnum from person

join vp_allperson on

person.personnum = vp_allperson.personnum

where vp_allperson.employmentstatus = 'Terminated' and person.fingerrequiredsw = 1;

I would like instead of returning my results of personnum, I would like to update  person.fingerrequiredsw from 1 to 0 if both the above conditions are true.

I am just not sure how?

Thank you for looking

David BassAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
I think I'm missing something.

For both of the conditions to be true, fingerrequiredsw is already 1 based on your where clause.
David BassAuthor Commented:
In my SQL statement, I am using a SELECT statement to show me the results of where both conditions are true.

It displays the personnum field if both conditions are true.  It does that just fine.

All this does is compare that both results are true.

My outcome is that I do not want these to be true.  

The first condition checks if a person is terminated, and they have a fingerrequired flag set to 1.  

I want to limit my fingerrequired to only active employees.  So if they are terminated then make this field,  person.fingerrequired from 1 to 0.  to 0 = off.  1 = on.

I hope that makes it clearer.

slightwv (䄆 Netminder) Commented:
I think I mis-read it originally.

I can think of two ways:
update person p1 set fingerrequiredsw = (
	select case when employmentstatus='Terminated' then 0 else 1 end from vp_allperson vp where p1.personnum=vp.personnum

Open in new window

merge into person p
	using (select * from vp_allperson) vp
	on (p.personnum=vp.personnum and vp.employmentstatus='Terminated')
when matched then update set p.fingerrequiredsw=0

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David BassAuthor Commented:
slightwv (䄆 Netminder) Commented:
Happy to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.