首页 > 代码库 > PL/SQL 块

PL/SQL 块

declare
 V_name emp.ename%TYPE :=‘SMITH‘;--定义成emp.ename类型,其初始化值必须在ename中存在
 V_job  varchar (20)  :=‘salesman‘;
 V_sal  number ;
 rec emp%rowtype;
begin
  select * into rec from emp where ename = V_name;
 DBMS_OUTPUT.PUT_LINE(‘姓名:‘||rec.ename||‘工资:‘||rec.sal||‘工作时间:‘||rec.hiredate); 
  select ename into V_name from emp where ename = ‘WARD‘;
 --dbms_output.put_line(‘姓名:‘||V_name);
 
 if V_name= ‘WARD‘ then
   DBMS_output.put_line(‘姓名:‘||V_name||rec.sal);
   
  
 end if;
 if V_name=‘JONES‘ then
   
  DBMS_output.put_line(‘Jones今天来上班了‘);
  else
    
  DBMS_output.put_line(‘Jones今天玩的真happy‘);
  
  end if;
  if V_name = ‘SCOTT‘ then
 DBMS_output.put_line(‘scott今天来上班了‘); 
  elsif  V_name = ‘十大队‘  then
 DBMS_output.put_line(‘ward今天来上班了‘);
 else      
  DBMS_output.put_line(‘今天没人上班,公司空空如也‘);  
  end if;
  
end;
--case的 应用练习  V_APPraisal :=的值为 case语句的返回值
 declare 
        V_grade char(1) :=‘A‘;
        V_APPraisal varchar2(20);
 begin
   V_APPraisal :=  --case内部不用加 ; 只有在结束时再加.
    case V_grade
     when ‘A‘  then ‘excellent‘
     when ‘B‘  then ‘vary good‘
     when ‘C‘ then  ‘GOOG‘
     else  ‘no such grade‘
   end;
   DBMS_OUTPUT.PUT_LINE(‘Grade:‘||V_grade||‘    Appraisal: ‘|| V_APPraisal);

 end;
 
 CREATE TABLE OWER (TIME date ,NAME varchar(10), NUM number(2));
declare
       INT number(2):= 1;
BEGIN 
  LOOP
    
       INT := INT +1;
       INSERT INTO OWER (TIME,NAME,NUM)
       VALUES (SYSDATE ,‘HHH‘,5);
       dbms_output.put_line (‘想看看你打印了‘||to_char(int)||‘次‘);
       dbms_output.put_line (int);
       exit when int=8;    --循环内容包含此处的值
   end loop;
end;
 
 select * from ower;
 declare 
    int number := 1;
 begin
   while int<10 loop --等于10 的时候退出
      dbms_output.put_line (‘想看看你打印了‘||to_char(int)||‘次‘);
      int:= int+1;
      end loop;
 end;
  create table temp_table(num_col number(10))  ;    
  DECLARE
        V_counter NUMBER := 10;

BEGIN
   INSERT INTO temp_table(num_col) VALUES (v_counter );
   FOR v_counter IN 20 .. 25 LOOP  -- 打印出20 ,到25 行的内容.
      INSERT INTO temp_table (num_col ) VALUES ( v_counter );
   END LOOP;
   INSERT INTO temp_table(num_col) VALUES (v_counter );
   FOR v_counter IN REVERSE 20 .. 25 LOOP
      INSERT INTO temp_table (num_col ) VALUES ( v_counter );
   END LOOP;
END ; 

 

PL/SQL 块