Oracle(pl/sql)

不管數據類型,直接用數據表的數據類型?

declare 

  v_id  stu.sid%type;

 v_name stu.sname%type;

begin 

 v_id:='&請輸入編號';

 select  sname  into  v_name  from stu  where  sid=v_id;

   dbms_output.put_line('取出的姓名:'||v_name);

end;

行的數據類型  %rowtype

declare
       v_id stu.sid%type;
       v_temp stu%rowtype;    
begin
       v_id:='&請輸入編號';
       select * into v_temp from stu where sid=v_id;
       dbms_output.put_line('編號:'||v_temp.sid||' 姓名:'||v_temp.sname||'  年齡:'||v_temp.sage);
end;

 

declare
       v_sid student.sid%type;
       v_date student.sdate%type;
begin
       v_sid := '&請輸入編號:';
       select sdate into v_date from student where sid=v_sid;
       --dbms_output.put_line('日期:'||v_date);
       dbms_output.put_line('日期:'||to_char(v_date,'yyyy"年"mm"月"dd"日" hh24:mi:ss dy'));
end;

 

-- 行類型 帶出一個新的問題:如果取出多行?用遊標 cursor解決多行問題!

declare
       v_gid stu.gid%type;    
.       v_temp stu%rowtype;    
begin
       v_gid:='&請輸入編號';
       select * into v_temp from stu where gid=v_gid;
       dbms_output.put_line('編號:'||v_temp.sid||' 姓名:'||v_temp.sname||'  年齡:'||v_temp.sage);
end;

declare
       --聲明一個類型
       type r_stu is record
       (
             v_id stu.sid%type,
             v_temp stu%rowtype
       );
       v_t r_stu;--v_t:變量 r_stu:數據類型 
begin
       v_t.v_id:='&請輸入編號';
       select * into v_t.v_temp from stu where sid=v_t.v_id;
       dbms_output.put_line('編號:'||v_t.v_temp.sid||' 姓名:'||v_t.v_temp.sname||'  年齡:'||v_t.v_temp.sage);
end;

 

 


/*
     多重if結構
*/
declare
     v_score number(3);
begin
     v_score:='&請輸入成績:';
     if(v_score>90) then
          dbms_output.put_line('優秀!');
     elsif(v_score>70) then
          dbms_output.put_line('中等!');
     elsif(v_score>60) then
          dbms_output.put_line('及格!');
     else
         dbms_output.put_line('請家長!');
     end if;
end;

 

/*
     case when then else end case;
*/
declare
     v_score number(3);
begin
     v_score:='&請輸入成績:';
     case
     when v_score>90 then dbms_output.put_line('優秀!');
     when v_score>70 then dbms_output.put_line('中等!');
     when v_score>60 then dbms_output.put_line('及格!');
     else
         dbms_output.put_line('請家長!');
     end case;
end;

 

/*
     循環:loop end loop;
     while和for
*/
declare
     v_i number(4);
begin
     v_i:=1;
     loop
          v_i:=v_i+1;
          exit when v_i>50;--退出的時候 
           dbms_output.put_line(v_i);  
     end loop;
end;

 

--while
declare
     v_i number(4);
begin
     v_i:=1;
     while(v_i<50) loop
           dbms_output.put_line(v_i);
           v_i:=v_i+1;--改變表達式值        
     end loop;
end;
 

--for
declare
begin
     for v_i in 1..50 loop
         dbms_output.put_line(v_i);
     end loop;
end;

 

--for實現 50到1依次輸出 reverse:逆向輸出
declare
begin
     for v_i in reverse 1..50 loop
         dbms_output.put_line(v_i);
     end loop;
end;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章