您的位置:首页 > 数据库 > Oracle

oracle之PL/SQL编程基础

2011-11-29 14:57 483 查看
--PL/SQL语言

--字符串连接符 ||
select 'abc' || '123' from dual;
 
--常量与变量
declare  --<<--用来定义 变量、常量、游标、自定义数据类型
stu_name nvarchar2(4); 
begin --<<--执行语句的开始
stu_name := '卢';  --   := 赋值符号
dbms_output.put_line(stu_name);
end;--<<--执行语句的结束

declare 
PI constant number := 3.1415926;  -- constant 用于定义常量

--定义数据类型
declare 
type student is record (  --<<--这里允许的数据类型除了 record(记录)还有 table(表) varray(变长数组)
name nvarchar2(20),
sex char(2),
age int);
stul student;  --<<--这里是在定义 student 类型的变量 stul
begin
stul.name := 'text';
dbms_output.put_line(stul.name);
end;

--IF... ELSE 语句

declare 
i number; --定义number类型的变量 i
begin
 i:= 1;
 if i < 18 then
  dbms_output.put_line('小于18');
 else 
  null;   --空语句,什么都不执行,起占位作用
 end if;
end;

-- IF... ELSIF... ELSE 语句

declare
i number;
begin
 i := 40;
 if i < 18 then
  dbms_output.put_line('小于18');
 elsif i > 18 and i < 30 then
  dbms_output.put_line('小于30');
 else 
  dbms_output.put_line('不在统计范围');
 end if;
end; 

--case 语句
declare 
c char(2);
begin
 c := 'C';
 case c      --<<--可以是表达式
  when 'A' then
   dbms_output.put_line('你得了A');
  when 'B' then
   dbms_output.put_line('你得了B');
  when 'C' then
   dbms_Output.put_line('你得了C');
  else
   dbms_output.put_line('很抱歉,你没及格');
 end case;
end;

--LOOP循环
declare
 i number;
begin
 i := 1;
 loop
  dbms_output.put_line(i);
  i := i + 1;
 exit when i > 5;  
 exit when i > 10;--<<--exit 语句可以有多条,不写的话则是死循环
 end loop;
end;

--WHILE 循环
declare
 i number;
begin
 i := 0;
 while i < 10 loop
  dbms_output.put_line(i);
  i := i + 1;
 end loop;
end;

--FOR 循环
declare
 i number;
 sumi number;
begin 
 i := 0;
 sumi := 100;
 for i in 1...100 loop
  sumi := sumi + i; 
  dbms_output.put_line(sumi);
 end loop;
 dbms_output.put_line(sumi);
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: