Chinaunix首页 | 论坛 | 博客
  • 博客访问: 47786
  • 博文数量: 19
  • 博客积分: 307
  • 博客等级: 二等列兵
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-03 21:01
文章分类
文章存档

2014年(6)

2012年(13)

我的朋友

分类: Oracle

2012-08-08 13:03:26

声明变量:名称,数据类型,定义标准变量或者,控制变量范围
变量规则,不能数字开头,长度不超过30个长度,不区分大小写,不能是系统关键字
结构declare
申明变量
begin
开始语句
end
/
set serveroutput on size 10000开启输出
执行以前的文本使用,@ /home/xxx
判断的if elsif  else endif
case模式为 case   when   then; else  end case;  end;
循环语句
loop end loop
declare
  2  x number;
  3  begin
  4  x:=0;
  5  loop
  6  x:=x+1;
  7  if x>=3 then
  8  exit;
  9  end if;
 10  dbms_output.put_line('x:'||x);
 11  end loop;
 12  dbms_output.put_line('x:'||x);
 13  end;
while 条件 loop end loop;
declare
  2  x number;
  3  begin
  4  x:=0;
  5  loop
  6  x:=x+1;
  7  if x>=3 then
  8  exit;
  9  end if;
 10  dbms_output.put_line('x:'||x);
 11  end loop;
 12  dbms_output.put_line('x:'||x);
 13* end;
for count in 开始数目,结束数目 loop  end loop
 declare
  2  begin
  3  for i in 1..5 loop
  4  dbms_output.put_line('i:内'||i);
  5  end loop;
  6  dbms_output.put_line('已经退出循环');
  7* end;
不和其他编程语言一样,是使用in,不是等于,如果要从大到小在in后面加上reverse
使用goto循环
 1  declare
  2  x number;
  3  begin
  4  x:=0;
  5  <>
  6  x:=x+1;
  7  dbms_output.put_line('x:内'||x);
  8  if x<3 then
  9  goto repeat_loop;
 10  end if;
 11* end;
在许多编程语言中,不推荐使用goto
异常处理:
异常结构:exception  when   then
 declare
  2  test varchar2(20);
  3  begin
  4  select dname into test from dept where deptno=800;
  5  dbms_output.put_line(test);
  6  exception
  7  when no_data_found then
  8  dbms_output.put_line('没找到数据');
  9* end;
有NO_DATA_FOUND,DUP_VAL_ON_INDEX(插入重复行),TOO_MANY_ROWS,VALUE_ERROR,ZARO_DIVID
自定义异常:declare
  2  tname varchar(40);
  3  e exception;
  4  begin
  5  select dname into tname from dept where deptno=20;
  6  if tname <> '123' then
  7  raise e;--抛出错误
  8  end if;
  9  dbms_output.put_line(tname);
 10  exception
 11  when e then
 12  dbms_output.put_line('不是找到的部门');
 13  end;
 14  /
定义复合变量,也成为记录,就是几个相关值构成的,就是返回一行数据的值
记录的声明:
type 记录名称 is record(
变量1 变量
变量2 类型);
引用一个记录,必须先申明 real_record 记录名
可以直接指定一个申明的字段为一个表里面的字段相同
如:id emp.uid%TYPE,引用这个表的字段
定义一个记录和一个表的东西一样
如:myrecortd emp%rowtype
 1  declare
  2  type myrecord is record(
  3  id varchar2(20),
  4  name varchar2(20));
  5  real_record myrecord;
  6  begin
  7  select empno,ename into real_record from emp where empno=1;
  8  dbms_output.put_line(real_record.id ||' ,'||real_record.name);
  9* end;
切记:插入、更新、删除后,必须commit;
阅读(672) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~