0

I'm newbie in SQL and i don't understand how this language work I have a code:

if (select Zarobki from PRACOWNICY) < 3000
    update PRACOWNICY set Zarobki = Zarobki * 2
else
    update PRACOWNICY set Zarobki = Zarobki / 2

How I can compare multiple values form table? I think select work like loop statements

3
  • What is your RDBMS? Commented Jan 27, 2019 at 9:46
  • MS SQL Management Studio Commented Jan 27, 2019 at 9:49
  • Just curious what kind of logic is it? If someone earns let's say 2000 then after update will get 4000, and person that has 5000 will get 2500. Commented Jan 27, 2019 at 11:59

2 Answers 2

5

You can write:

update PRACOWNICY
set Zarobki = CASE
               when Zarobki < 3000
                then Zarobki * 2
               else
                then Zarobki / 2
              END
Sign up to request clarification or add additional context in comments.

Comments

1

this kind of operation can be done with one query.

update PRACOWNICY 
set Zarobki = 
  case when Zarobki < 3000 then 
    Zarobki * 2 
  else 
    zarobki /2 
  end

This query is equivalent to

update PRACOWNICY set Zarobki = Zarobki * 2
where Zarobki < 3000;

update PRACOWNICY set Zarobki = Zarobki / 2
where Zarobki >= 3000;

2 Comments

yes because your example code has else statement :) what do you want to do? please update your question and provide sample data and expected output so that we can understand what you are up to
I modify your code and it work. Just change 'else' on second 'when -than'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.