how to use if contions in update statement

i need to use if condition in update query,please check and let me know the below update statement will work fine??

if opt=1 then
update EOD SET  flag='Y' where Step_name='STEP_1'
else if
ip opt=2 then
update EOD SET  flag='Y' where Step_name='STEP_2'
end if;

'''

Upvotes: 0

Views: 126

Answers (2)

Prakaash
Prakaash

Reputation: 89

CREATE
    OR REPLACE PROCEDURE archana (opt IN NUMBER) IS
BEGIN
    IF opt = 1 THEN
        UPDATE EOD SET FLAG = 'Y' WHERE STEP_NAME = 'STEP_1';
    ELSIF opt = 2 THEN
        UPDATE EOD SET FLAG = 'Y' WHERE STEP_NAME = 'STEP_2';
    ELSE
        dbms_output.put_line('ERROR');
    END IF;
END;

opt is accepted as input parameter then based on the value corresponding update queries are executed. P.S: kindly test your code before posting in this forum instead of asking whether it will work. if its not working then many peeps are here to help.

Upvotes: 0

You can do it with where clause.

update EOD SET  flag='Y' where (Step_name=STEP_1 and opt=1) or(Step_name=STEP_2 and opt=2);

To do it in PL/SQL as you intended instead of else if you need to use elseif:

if opt=1 then
update EOD SET  flag='Y' where Step_name=STEP_1;

elseif opt=2 then
update EOD SET  flag='Y' where Step_name=STEP_2;
end if;

Upvotes: 1

Related Questions