Oracle PL/SQL基本语法练习

详细链接 http://blog.csdn.net/acmman/article/details/52422492
– 查询

select * from myemp;

– 通过as创建表

create table empvd20 as select * from emp041301;

– 创建数据源

create table EMP0413(EMPNO NUMBER,
                     ENAME VARCHAR2(10),
                     JOB VARCHAR2(9),
                     MGR NUMBER,
                     HIREDATE DATE,
                     SAL BINARY_DOUBLE,
                     COMM BINARY_DOUBLE,
                     DEPTNO NUMBER);
create table dept0413(DEPTNO NUMBER, DNAME VARCHAR2(50));

– 插入数据一种方法

INSERT INTO EMP041301
SELECT 1110, '张三', '主管', 1110, to_date('12-3-14', 'dd-MM-yy'), 5200, 0, 20 FROM dual 
UNION ALL
SELECT 1111, '李四', '销售', 1116, to_date('03 - 11 - 15', 'dd-MM-yy'), 3400, 500, 30 FROM dual
UNION ALL
SELECT 1112, '王五', '销售', 1116, to_date('25 - 4 - 12', 'dd-MM-yy'), 4400, 800, 30 FROM dual 
UNION ALL
SELECT 1113, '赵二', '后勤', 1110, to_date('30-5-11', 'dd-MM-yy'), 3450, 0, 40 FROM dual 
UNION ALL
SELECT 1114, '李磊磊', '会计', 1110, to_date('22-12-15', 'dd-MM-yy'), 2500, 0, 50 FROM dual 
UNION ALL
SELECT 1115, '张少丽', '销售', 1110, to_date('11-3-16', 'dd-MM-yy'), 2400, 1400, 30 FROM dual 
UNION ALL
SELECT 1116, '林建国', '主管', 1116, to_date('22-1-16', 'dd-MM-yy'), 5700, 0, 20 FROM dual 
UNION ALL
SELECT 1117, '马富邦', '后勤', 1116, to_date('22-7-13', 'dd-MM-yy'), 2800, 0, 40 FROM dual 
UNION ALL
SELECT 1118, '沈倩', '会计', 1116, to_date('06-5-10', 'dd-MM-yy'), 2100, 0, 50 FROM dual;
commit;

– 第二种 增删改 需要提交才能数据改变;

insert into t(c1,c2) values(date'2010-2-12',timestamp'2010-2-12 13:24:52.234123211');

insert into EMP0413 values(1110,'张三','主管',1110, to_date('12-3-14','dd-MM-yy'),5200,0,20);
commit;

– 表 dept0413
20,‘管理部门’
30,‘销售部门’
40,‘后勤部门’
50,‘金融部门’

insert into dept0413 values(20,'管理部门');
insert into dept0413 values(30,'销售部门');
insert into dept0413 values(40,'后勤部门');
insert into dept0413 values(50,'金融部门');
commit;

– 创表结束
select to_char(hiredate,‘yyyy’) from emp0413;
select count(1) from emp0413;

– (1)实例1:统计每年入职的员工个数
– 计数器可以实现,分组求和的目的;
select to_char(hiredate,‘yyyy’) 年份 ,count(1) 入职员工数 from emp0413 group by to_char(hiredate,‘yyyy’) order by 年份 ;

– pl/sql语法

declare
   cursor empCount is select to_char(hiredate,'yyyy') from emp0413;
   year varchar2(4);
    --计数器(2010-2016的入职人数统计)
    count10 number := 0;
    count11 number := 0;
    count12 number := 0;
    count13 number := 0;
    count14 number := 0;
    count15 number := 0;
    count16 number := 0;
begin
  open empCount;
       loop 
         fetch empCount into year;
         exit when empCount%notfound;
         if year = '2010'  then count10 := count10+1; 
           elsif year = '2011'  then count11 := count11+1;
           elsif year = '2012'  then count12 := count12+1;
           elsif year = '2013'  then count13 := count13+1;
           elsif year = '2014'  then count14 := count14+1;
           elsif year = '2015'  then count15 := count15+1;
           else count16 := count16+1;
         end if; 
       end loop;
  close empCount;
  dbms_output.put_line('2010~2016年入职总人数数:'||(count10+count11+count12+count13+count14+count15+count16));
  dbms_output.put_line('2010年入职人数:'||count10);
  dbms_output.put_line('2011年入职人数:'||count11);
  dbms_output.put_line('2012年入职人数:'||count12);
  dbms_output.put_line('2013年入职人数:'||count13);
  dbms_output.put_line('2014年入职人数:'||count14);
  dbms_output.put_line('2015年入职人数:'||count15);
  dbms_output.put_line('2016年入职人数:'||count16);    
end;

– (2)实例2:涨工资
为员工涨工资,从最低工资调起,每人涨10%,但工资总额不能超过5万元,
请计算涨工资的人数和涨工资后的工资总额,并输出涨工资人数和工资总额。

– pl/sql编程

declare
   cursor pc is select empno,sal from emp0413 order by sal;
   pEmpno emp0413.empno%type;--个人编号
   pCount number:=0;-- 涨工资人数
   pSal emp0413.sal%type; -- 个人薪水
   sumSal number(8);-- 总薪水 
begin
  select sum(sal) into sumSal from emp0413;
  open pc;
     loop
       exit when sumSal + pSal * 0.1 > 50000;
       fetch pc into pEmpno,pSal; 
       exit when pc%notfound;
       dbms_output.put_line('pEmpno:'||pEmpno);
       update emp0413 e set e.sal = e.sal+0.1*e.sal where e.empno = pEmpno ; 
       pCount := pCount + 1;
       sumSal := sumSal+pSal*0.1; 
     end loop;
  close pc;
  
  dbms_output.put_line('涨工资人数:'||pCount);
  dbms_output.put_line('工资总额:'||sumSal); 
end;

–验证 没提交可以回滚;
select * from emp0413 order by deptno ;
commit;

–(3)实例3:统计工资段
用PL/SQL语言编写一程序,实现按部门分段(6000以上、(6000,3000)、3000元以下)
统计各工资段的职工人数、以及各部门的工资总额(工资总额不包括奖金),参考如下格式:

select distinct deptno from emp0413;
select * from dept0413;

declare
       cursor pc1 is select DEPTNO from dept0413;
       cursor pc2(inDeptNo dept0413.deptno%type) is select SAL from emp0413 where DEPTNO = inDeptNo; 
       count1 number;
       count2 number;
       count3 number;
       pSal  emp0413.sal%type;
       sumSal number;
       curDeptNo dept0413.DEPTNO%type;
         
begin
  dbms_output.put_line('部门 小于3000数 3000-6000 大于6000 工资总额');
  open pc1;
       loop
         fetch pc1 into curDeptNo;
         exit when pc1%notfound;
         select sum(sal) into sumSal from emp0413 where DEPTNO = curDeptNo;
         count1 :=0;
         count2 :=0;
         count3 :=0;
         open pc2(curDeptNo);
              loop
                fetch pc2 into pSal;
                exit when pc2%notfound;
                if pSal >=6000 then 
                  count1 := count1+1;  
                elsif pSal>=3000 and pSal < 6000 then
                  count2 := count2+1;
                else
                  count3 := count3+1; 
                               
                end if; 
      
              end loop;
         
         close pc2;
         dbms_output.put_line(curDeptNo||' '||count3||' '||count2||' '||count1||' '||sumSal);
         
       end loop; 
  close pc1;
end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值