当前位置:数据库 > SQLServer >>

PL/SQL的应用操作实例以及解析

PL/SQL的应用操作实例以及解析
 
[sql] 
declare  
  msg char(15);--定义变量  
  begin  
   msg:='hell';--赋值  
   dbms_output.put_line(msg);  
   end; 
  
  
--声明的类型为 与emp.sal类型变量一致  
  
declare  
   sal emp.sal%type; --sal和emp表中sal类型一样  
   begin  
     sal:=900.44;  
     dbms_output.put_line(sal);  
     end;  
       
--记录一行  
declare  --声明  
  emp_rec emp%rowtype;--emp_rec 是一个变量  emp表的一行记录    emp%rowtype  
  begin  
  select * into emp_rec from emp where empno=7499; --给emp_rec 赋值的时候必须给他一条记录  into语句表示赋值  
  dbms_output.put_line(emp_rec.sal);--不能直接输出emp_rec  需要emp_rec.sal  
  end;  
       
    
--if语句形式1  
declare  
 grade number(5,2);  
 begin  
   grade:=80;  
   if grade>70 then  
     dbms_output.put_line('greate');  
     end if;  
     end;  
       
       
       
--if语句形式2  
declare  
 grade number(5,2);  
 begin  
   grade:=60;  
   if grade>70 then  
     dbms_output.put_line('greate');  
   else  
     dbms_output.put_line('bad');  
     end if;  
     end;  
       
         
--if语句形式3  
declare  
 grade number(5,2);  
 begin  
   grade:=60;  
   if grade>70 then  
     dbms_output.put_line('greate');  
   elsif grade>50 then  
     dbms_output.put_line('bad');  
     else  
      dbms_output.put_line('bad2');  
     end if;  
     end;     
       
 accept num prompt '请输入数字';--接收键盘输入的值  会自动保存num的值  
 declare--声明  
   pnum number :=#-- &代表取值 pnum  格式 声明变量  变量类型   
   begin  
     dbms_output.put_line(pnum);  
    end;  
      
      
    declare  
     pnum number :=1;  
     begin  
     while pnum<10 --while语句   exit when pnum>10  循环退出的条件  
     loop  
      dbms_output.put_line(pnum);  
      pnum:=pnum+1;--循环改变的条件  
       end loop;  
    end;  
   
 declare  
  begin  
    for i in 1..10   
    loop  
    dbms_output.put_line(i);  
    end loop;  
    end;  
      
      
 select * from emp;  
   
 declare   
  job_rec emp.job%type;  
  begin  
    select job into job_rec from emp;  --存在问题 返回的job岗位为多个,而job_rec只能接受一个值  
    if job_rec ='MANAGER' then  
       update emp set sal = sal+800;   --没有指定where子句  
       elsif job_rec='SALESMAN' then  
        update emp set sal=sal+400;  
        else  
          update emp set sal = sal+200;  
        end if;  
        end;  
      
      
      
   --声明游标  
    
   declare  
   cursor cl is select * from emp;--查询出所有的emp记录存储到cl游标中  
    emp_rec emp%rowtype;  
    begin  
     open cl;--打开游标  
       loop  
       exit when cl%notfound; --循环退出的条件 cl%notfound 当没有记录的时候 返回true  
     fetch cl into emp_rec;--检索数据  
      dbms_output.put_line(emp_rec.empno);  
      end loop;  
      close cl;  
      end;  
      
  declare   
     emp_row emp%rowtype;  
     begin  
       select * into emp_row from emp; --隐式游标  
                                  --例外的声明       
       exception  
           
         when too_many_rows then    --when 例外的名称 then 发生例外执行的操作  
         dbms_output.put_line('超了');  
        end;    
          
          
   declare  
        no_data exception;  
        emp_row emp%rowtype;  
        cursor cl(pno number) is select *  from emp where empno=pno;  
        begin  
          open cl(0000);  
          fetch cl into emp_row;  
          if cl%notfound then raise no_data; --在这里抛出例外  
          end if;  
          close cl;  
   
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,