2018.5.27 OraclePLSQL編程 if-else練習和循環結構練習


##if-else運用

declare 
  v_num number(8); 
begin
v_num :=&n;
  if v_num>0 and v_num<100 then
     dbms_output.put_line('工薪階級'); 
  elsif v_num between 100 and 200 then
  dbms_output.put_line('中產階級'); 
  else 
  dbms_output.put_line('資本家');
  end if;  
end;

##case運用

declare
  v_num number(8) :=&n;
  v_result varchar2(15);
begin
  case
     when v_num =1 then v_result :='剪刀'; 
     when v_num =2 then v_result :='石頭';
     when v_num =3 then v_result :='布';
     else
          v_result :='輸入的數字無效';
     end case;
     dbms_output.put_line(v_result);
end;

##--案例:1-10輸出(loop循環實現)

declare
  i number(8) :=1;
begin
  loop
    dbms_output.put_line(i);
    exit when i =10;
    --改變初始值
    i := i+1;
    end loop;
end;
/

##--while循環實現上面例子

declare
  i number(8) :=1;
begin
  while i<=10 loop
    dbms_output.put_line(i);
    --改變初始值
    i := i+1;
    end loop;
end;
/

##--for循環輸出1-10 ###結構特點

for i in() loop
end loop;
declare
    --i number(8) :=&n;
begin
  for i in 1..10 loop  --初始值..結束值
  dbms_output.put_line();      
end loop;
end;
/

###for循環反向輸出 10-1

declare
    --i number(8) :=&n;
begin
  for i in resever  1..10 loop  --初始值..結束值
  dbms_output.put_line(i);      
end loop;
end;
/
declare 
v_a number(8);
begin
v_a :=1;
if v_a= 1 then 
dbms_output.put
declare
 num number(8)  :=&n;
 result number(8);
 --f(n) number(8);
begin 
    case 
    when num=1 then result :=1;
    when num=2 then result =result

##作業if-else 結構

1、輸入員工的編號,判斷此員工的工資: -如果工資sal<1000則讓工資在原來在基礎上加上100 -如果工資1000<= sal <2000則讓工資在原來在基礎上加上200 -否則讓員工工資在原來的基礎上加上300;

declare
   v_empno number(30) :=&n;
   v_sal number(30);
begin
   select sal into v_sal from emp where empno=v_empno; 
   if v_sal<1000 then v_sal :=v_sal +100;
   elsif v_sal between 1000 and 2000 then v_sal:=v_sal+200;
   else 
       v_sal :=v_sal+300;  
   end if;  
   dbms_output.put_line(v_sal);
end;
/

2.編寫一個過程,可以輸入一個雇員名,如果該雇員的工資低於2000,就給該員工工資增加10%。(使用if)

declare
 v_ename varchar2(30) :=&n;
 v_sal number(30); 
begin 
 select sal into v_sal from emp where ename=v_ename;
  if v_sal<2000 then v_sal :=v_sal+(v_sal*0.1);
  end if;
  dbms_output.put_line(v_sal);
end;
/

3.編寫一個過程,可以輸入一個雇員名,如果該雇員的補助不是0 就在原來的基礎上增加100;如果補助為0 就把補助設為200;(使用if...else)

declare 
    v_ename varchar2(20) :=&n;
    --v_money number(30);
    v_nvl number(20);                         
begin 
    select nvl(comm,0) into v_nvl from emp where ename=v_ename;
    if(v_nvl =0) then v_nvl :=v_nvl+200; 
    else v_nvl := v_nvl+100;
    end if;
    dbms_output.put_line(v_nvl);
end;
/

--4、編寫一個過程,可以輸入一個雇員編號,如果該雇員的職位是PRESIDENT就給他的工資增加1000, --如果該雇員的職位是MANAGER 就給他的工資增加500,其它職位的雇員工資增加200。(使用if..elsif..else)

declare
   v_empno number(32) :=&n;  
   v_job varchar2(20); 
   v_sal number(20);             
begin
  select job,sal into v_job,v_sal from emp where empno=v_empno;
  if(v_job ='PRESIDENT') then v_sal :=v_sal+1000;
  elsif (v_job = 'MANAGER') then v_sal :=v_sal+500;
  else v_sal :=v_sal+200;
  end if;
  dbms_output.put_line(v_sal);
end;
/

##作業循環結構

1、循環輸出“haha1...haha10”(使用while)

declare
   i number(5) :=1;
begin
  while i<10 loop
  dbms_output.put_line('haha' || i);
  i :=i+1;
  end loop; 
end;
/  

2、把上述示例改為loop實現

declare
   i number(5) :=1;
begin
  loop
  dbms_output.put_line('haha' || i);
  exit when i =10;
  i :=i+1;
  end loop; 
end;
/  

3、現有一張表users,字段(uid,uname),分別使用(loop、while、for完成)。 請編寫一個過程,可以輸入用戶名,並循環添加10 個用戶到users 表中,用戶編號從1 開始增加。

create table users  
(  
    userid number(8),  
    uname varchar2(20)  
);  


  
declare  
     v_id number(8):=1;  
     v_name varchar2(20);  
begin  
     while v_id<=3 loop  
       v_name:='&name';  
       insert into users values(v_id,v_name);  
       v_id:=v_id+1;  
     end loop;  
end;  
/    

4、打印九九乘法表

這個還要修改

declare
v_result number(20);
   --i number(20) :=1; 
begin
   for i in 1..9  loop;
     for j in 1..9 loop;
     v_result :=i*j;
     if length(i*j)=1 and j!=1 then  
         dbms_output.put_line(' ');  
     end if;  
       dbms_output.put_line(i || '*'||j ||'='||v_result);
        dbms_output.put_line(' ');  
  end loop;
end;

這是對的

declare  
begin  
   for i in 1..9 loop  
     for j in 1..i loop  
        dbms_output.put(i);   
          dbms_output.put('*');   
            dbms_output.put(j);   
             dbms_output.put('=');   
              if length(i*j)=1 and j!=1 then  
                 dbms_output.put(' ');  
                 end if;  
              dbms_output.put(i*j);   
              dbms_output.put(' ');    
     end loop;  
   dbms_output.put_line(' ');  
   end loop;  
end;  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM