Oracle基础教程知识点总结(二)

2014-11-24 17:55:24 · 作者: · 浏览: 5


1.标量类型(scalar)

2.复合类型(composite)

3.参考类型(reference)

4.clob(large object)

1.标量

v_name varchar2(20); --变长字符串

v_sal number(6,2); ---9999.99~+9999.99

v_sql2 number(6,2):=5.4; --定义一个小数并赋初值为5.4,":="相当于java中的"="赋值运算符;

v_hiredate date; --定义一个日期型数据

v_valid boolean not null default false;

eg1:--把'wucaiming'的工资涨一倍,并输出

set serveroutput on;

declare


v_name varchar2(30);-- v_name mytable.name%type;

v_sal_add number(6,2);

begin

select name,sal*2 into v_name ,v_sal_add from mytable where name=&name;

dbms_output.put_line('姓名:'||v_name||' 工资:'||v_sal_add);

end;


2.复合变量

pl/sql记录

pl/sql表

嵌套表

varray

--pl/sql记录

类似于高级语言中的结构体(或者类)

set serveroutput on;

--定义一个plsql 记录 类型;

declare type mytable_record_type is record(

v_name mytable.name%type,

v_passwd mytable.passwd%type,

v_sal mytable.sal%type

);

--定义了一个变量,变量的类型是上面定义的记录类型;

r_record mytable_record_type;

begin


select name,passwd,sal into r_record from mytable where name='wucaiming';

dbms_output.put_line('姓名:'||r_record.v_name||' 密码:'||r_record.v_passwd||' 工资:'||r_record.v_sal);

end;

--pl/sql表

表相当于高级语言中的数组。这里的数组下标可以为负;

set serveroutput on;

declare type mytale_table_type is table of mytable.name%type index by binary_integer;

t_table mytale_table_type;

begin

select name ,passwd ,sal into t_table(-1),t_table(0),t_table(1) from mytable where name='wucaiming';

dbms_output.put_line('姓名:'||t_table(-1)||' 密码:'||t_table(0)||' 工资:'||t_table(1));

end;

3.参照变量 之游标变量

定义游标不需要select语句,使用游标需要使用select语句;

declare


type mytable_cursor is ref cursor;

test_cursor mytable_cursor;

v_name mytable.name%type;

v_passwd mytable.passwd%type;

v_sal mytable.sal%type;

begin

--游标与一个select关联;

open test_cursor for select name,passwd,sal from mytable where name='wucaiming';

loop

fetch test_cursor into v_name,v_passwd,v_sal;

--判断游标是否为空;

exit when test_cursor%notfound;

dbms_output.put_line('姓名:'||v_name||' 密码:'||v_passwd||' 工资:'||v_sal);

end loop;

end;


pl/sql的高级用法:(能编写高级过程,下订单过程模块)

pl/sql的控制结构:

if...then


if...then...else

if...then...elsif...then...else


eg:

create or replace procedure p_addsal(name1 varchar2) is

v_sal mytable.sal%type;

begin

select sal into v_sal from mytable where name=name1;

if v_sal <2000 then

update mytable set sal=200 where name=name1;

end if;

end;


二层条件分支:

eg:

create or replace procedure p_addsal(name1 varchar2) is

v_sal mytable.sal%type;

begin

select sal into v_sal from mytable where name=name1;

if v_sal <200 then

update mytable set sal=200 where name=name1;

else


update mytable set sal=v_sal+100 where name=name1;



end if;

end;

三层分支:

eg:


create or replace procedure p_addsal(name1 varchar2) is

v_sal mytable.sal%type;

begin

select sal into v_sal from mytable where name=name1;

if v_sal >=300 then

update mytable set sal=v_sal+150 where name=name1;

elsif v_sal>=200 then


update mytable set sal=v_sal+100 where name=name1;

else

update mytable set sal=v_sal+50 where name=name1;



end if;

end;