본문 바로가기

Database/PLSQL

009 - [Oracle PL/SQL] While Loop

<샘플코드에서 사용한 데이터는 HR 스키마이고, 오라클 설치시 생성할 수 있는 기본 스키마 입니다>

 

 

declare
    v_counter number:=1;
begin

    while v_counter<=3
    loop
    dbms_output.put_line('welcome');
    v_counter:=v_counter+1;
    dbms_output.put_line('after v_counter+1: '|| v_counter);
    end loop;

end;
----------------------------------------------------------------
welcome
after v_counter+1: 2
welcome
after v_counter+1: 3
welcome
after v_counter+1: 4

 

 

 

# print the employees first name for employee 100,101,102 
# using while loop

declare
    v_empno number:=100;
    v_first_name employees.first_name%type ;
begin
    while v_empno<=102
    loop
    select first_name into v_first_name
    from employees
    where employee_id = v_empno;
    
    dbms_output.put_line(v_empno ||' '|| v_first_name);
    
    v_empno:=v_empno+1; 
    
    end loop;
end;
---------------------------------------------------------
100 Steven
101 Neena
102 Lex