Chinaunix首页 | 论坛 | 博客
  • 博客访问: 24977
  • 博文数量: 25
  • 博客积分: 1010
  • 博客等级: 少尉
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-22 20:04
文章分类

全部博文(25)

文章存档

2011年(1)

2009年(24)

我的朋友
最近访客

分类: Oracle

2009-08-14 17:07:23

------------plsql
plsql是oracle对sql语言的扩展,就是在sql语言基础上加入了逻辑循环和判断。


-----------plsql块结构
declare----声明部分
  
begin------执行部分

  exeption--异常处理

end


注意:执行部分是必须的,其他都是可选的


-----------plsql块分类
匿名块
命名块:存储过程,函数,触发器,包


----------定义变量
declare
v_a  number:=250;
v_b  char(10) default '男';
v_c  date;
c_d  constant number:=3.14;
v_e  varchar2(20) not null:='i love this game';
begin
v_c:=sysdate;
dbms_output.put_line(v_a);
dbms_output.put_line(v_b);
dbms_output.put_line(v_c);
dbms_output.put_line(c_d);
dbms_output.put_line(v_e);
end;
/


注意:default只能在声明部分设置默认值
     使用dbms_output打印必须设置set serveroutput on
     constant 是常量(程序中不允许改变的量)


------------打印学号为2000012的姓名,性别,年龄
declare
v_sname char(9);
v_ssex  char(2);
v_sage  number;
begin
select sname,ssex,sage into v_sname,v_ssex,v_sage
from student
where sno='2000012';
dbms_output.put_line(v_sname||v_ssex||v_sage);
end;
/

 

注意:plsql中不允许直接写select,如果写必须带into
     使用select...into...赋值,select语句所返回的行数必须,并且,只能是一行。


-------------打印员工表中manager_id为null的员工姓名,工资,雇佣日期


--------------%type
表名.列名%type

declare
v_sname student.sname%type;
v_ssex  student.ssex%type;
v_sage  student.sage%type;
begin
select sname,ssex,sage into v_sname,v_ssex,v_sage
from student
where sno='2000012';
dbms_output.put_line(v_sname||v_ssex||v_sage);
end;
/


-----------------%rowtype
表名%rowtype

declare
v_stu  student%rowtype;
begin
select * into v_stu
from student
where sno='2000012';
dbms_output.put_line(v_stu.sname||v_stu.ssex||v_stu.sage);
end;
/


注意:复合变量在输出的时候必须打点输出

 

----------------判断语句
-------if
if  条件  then
语句;
elsif 条件 then
语句;
elsif 条件 then
语句;
...
else
语句;
end if;

--------------------打印King的工资级别
>= 1000  一级
>= 5000  二级
>= 10000 三级
>= 15000 四级
20000以上 五级


declare
v_salary  employees.salary%type;
begin
select salary into v_salary
from employees
where last_name='King';
if v_salary>=20000 then
dbms_output.put_line('五级工资');
elsif v_salary>=15000 then
dbms_output.put_line('四级工资');
elsif v_salary>=10000 then
dbms_output.put_line('三级工资');
elsif v_salary>=5000 then
dbms_output.put_line('二级工资');
elsif v_salary>=1000 then
dbms_output.put_line('一级工资');
else
dbms_output.put_line('工资级别太低');
end if;
end;
/
---------------------打印2000012学生所有课平均分的成绩级别
>=90 优秀
>=80 良好
>=70 中等
>=60 及格
其他 不及格

declare
v_avg number;
begin
select avg(grade) into v_avg
from sc
where sno='2000012';
if v_avg>=90 then
        dbms_output.put_line('优秀');
elsif v_avg>=80 then
 dbms_output.put_line('良好');   
elsif v_avg>=70 then
 dbms_output.put_line('中等');   
elsif v_avg>=60 then
 dbms_output.put_line('及格');   
else
 dbms_output.put_line('不及格');
end if;
end;
/

---------------------case
case when 条件 then 语句;
     when 条件 then 语句;
     when 条件 then 语句;
     ...
else 语句;
end case;

 

declare
v_avg number;
begin
select avg(grade) into v_avg
from sc
where sno='2000012';
case when v_avg>=90 then dbms_output.put_line('优秀');
     when v_avg>=80 then dbms_output.put_line('良好');
     when v_avg>=70 then dbms_output.put_line('中等');
     when v_avg>=60 then dbms_output.put_line('及格');
else  dbms_output.put_line('不及格');
end case;     
end;
/


--------------------循环
---------loop...end loop
loop
语句;
if 条件 then
exit;
end if;
end loop;
------------------
loop
语句;
exit when 条件;
end loop;
---------------------
declare
n number:=1;
begin
loop
n:=n+1;
exit when n=10;
end loop;
dbms_output.put_line(n);
end;
/

----------打印1到5
declare
n number:=1;
begin
loop
dbms_output.put_line(n);
exit when n=5;
n:=n+1;
end loop;
end;
/

------------作业打印1到5的和
------------作业打印1到5的偶数和

---------------while
while 条件 loop
语句;
end loop;


declare
n number:=1;
begin
while n<10 loop
n:=n+1;
end loop;
dbms_output.put_line(n);
end;
/
------------打印1到5
declare
n number:=1;
begin
while  n<=5    loop
dbms_output.put_line(n);
n:=n+1;
end loop;
end;
/

------------作业打印1到5的和
------------作业打印1到5的偶数和


-----------for
for i in 下限 .. 上限 loop
语句;
end loop;


------------打印1到5
begin
for i in 1..5 loop
dbms_output.put_line(i);
end loop;
end;
/

------------打印5到1
begin
for i in reverse 1..5 loop
dbms_output.put_line(i);
end loop;
end;

/

阅读(345) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~