PLSQL使用 QWLJ-S6UT-WC93-5XW7-WK3Q-F7KA-SBEU-XL

--定义变量的方法,以及两种赋值方法
declare
       emp_sal number(7,2):=800;
       max_sal number(7,2);
       emp2_sal CONSTANT number(7,2) :=780; --常量
begin
  select max(sal) into max_sal
  from scott.emp;
  dbms_output.put_line(emp_sal);
  dbms_output.put_line(max_sal);
end;

--演示%type %rowtype的使用
declare
       emp_sal scott.emp.sal%TYPE:=800;
       max_sal number(7,2);
       emp2_sal CONSTANT number(7,2) :=780; --常量
       emp_row scott.emp%ROWTYPE; --定义了可以接收一行数据的类型
       
begin
  select max(sal) into max_sal
  from scott.emp;
  
  select * into emp_row from scott.emp 
  where sal=max_sal;
  
  dbms_output.put_line(emp_sal);
  dbms_output.put_line(max_sal);
  dbms_output.put_line(emp_row.ename||emp_row.empno);--输出这一行中的列值
  --dbms_output.put_line(emp_row);
end;

--演示一下if ---then ----elsif
--输入一个雇员名字,如果是项目经理+2000 or 软件开发工程师1000 or部门经理+3000
declare 
v_position tsm_employee.position%TYPE;
v_emp_no tsm_employee.emp_no%TYPE;
begin
  select position,emp_no into v_position ,v_emp_no
            from tsm_employee where name='&请输入雇员姓名';
  if v_position='项目经理'
    then 
     update tsm_employee set salary=salary+2000 where 
                                   emp_no = v_emp_no;
    elsif v_position='软件开发工程师'
     then 
       update tsm_employee set salary=salary+1000 where 
                                   emp_no = v_emp_no;   
     elsif v_position='部门经理'
        then
        update tsm_employee set salary=salary+3000 where 
                                   emp_no = v_emp_no; 
    end if;
   
    commit;
    exception when others then    --others代表一切异常
    dbms_output.put_line('发生错误'||sqlerrm);
    rollback;
   end;
        
        select * from tsm_employee                       
                                                                   
--case when ,sqlserver可以写在select语句中??
begin
   case '&salary'
     when 15000 then dbms_output.put_line('还可以');
     when 8000 then dbms_output.put_line('过的去');
     when 5000 then dbms_output.put_line('活不了');
     else dbms_output.put_line('想死了');
     end case;   
end;  


/*loop  语法规则:
                   loop
                      exit 退出条件
                      要循环语句;
                   end loop;
 
 */
declare 
 i int :=1;
 j int :=1;
begin
   loop 
     exit when i>4;   --i大于4将退出
      loop
        exit when j>i;
          dbms_output.put('*');
          j:=j+1;
      end loop;
      dbms_output.new_line();
      i:=i+1;
      j:=1;
    end loop;
end;      
 
/*
   while

*/      
declare 
 i int :=1;
 j int :=1;

begin
  while i<=4 loop
     while j<=i loop
        dbms_output.put('*');
        j:=j+1;
     end loop;
     i:=i+1;
     j:=1;
   end loop; 
end;         

/*
  for
*/   
declare 
 i int :=1;
 j int :=1;
begin
  for i in 1..4 loop
     for j in 1..i loop 
       dbms_output.put('*');
        
      end loop;
       dbms_output.new_line();
   end loop;
end;    

 

posted @ 2012-10-19 12:19  邹晟  阅读(2548)  评论(0编辑  收藏  举报