plsql的一般结构体:
declare(可选,声明各种变量和游标的地方)
begin(必要的,从此开始执行)
exception(抓取到异常后执行的)
end;
[sql] view plaincopy
set serveroutput on;(默认是关闭)
---简简单单的plsql输出
begin
dbms_output.put_line('HelloWorld!');
end;
---定义一本字符串变量,并输出,oracle中的:=是赋值,= 是运算符(类似>,<)
declare
v_name varchar2(20);
begin
v_name := 'myname';
dbms_output.put_line(v_name);
end;
---
declare
v_num number := 0;
begin
v_name := 2/v_num;
dbms_output.put_line(v_name);
exception
when others then
dbms_output.put_line('error');
end;
--变量声明
注:每一行只能声明一个变量
--常用变量类型
1. binary_integer: 整数,主要用来计数而不是用来表示字段类型,效率高
2. number: 数字类型
3. char: 定长字符串
4. varchar2: 变长字符串
5. date: 日期
6. long: 长字符串,最长2GB
7. boolean: 布尔类型,可以取值为ture、false和null值,一定要给初值,
--变量声明
[sql]
declare
v_temp number(1);
v_count binary_integer := 0;
v_sal number(7,2) := 4000.00;
v_date date := sysdate;
v_pi constant number(3,2) := 3.14;
v_valid boolean := false;
v_name varchar2(20) not null := 'MyName';
begin
dbms_output.put_line('v_temp value:' || v_count);
end;
--变量声明,使用%type属性
[sql]
/*注释多行*/ --注释一行
declare
v_empno number(4);
v_empno2 emp.empno%type;--用表内字段类型声明变量类型
v_empno3 v_empno2%type;--用变量type属性声明变量类型
begin
dbms_output.put_line('Test');
end;
--简单变量赋值
[sql]
declare
v_name varchar2(20);
v_sal number(7,2);
v_sal2 number(7,2);
v_valid boolean := false;
v_date date;
begin
v_name := 'MyName';
v_sal := 23.77;
v_sal2 := 23.77;
v_valid := (v_sal = v_sal2);--判断是否相等用=
v_date := to_date('1999-08-12 12:23:30','YYYY-MM-DD HH24:MI:SS');
end;
--Table变量类型,表示一个数组
[sql]
declare
type type_table_emp_empno is table of emp.empno%type index by binary_integer;--首先声明一个类型
v_empnos type_table_emp_empno;--再用这个类型声明变量
begin
v_empnos(0) := 7369;
v_empnos(2) := 7839;
v_empnos(-1) := 9999;
dbms_output.put_line(v_empnos(-1));---1访问不到,0,2可以访问到
end;
--Record类型,表示一个类
[sql]
declare
type type_record_dept is record
(
deptno dept.deptno%type,
dname dept.dname%tpye,
loc dept.loc%type
);
v_temp type_record_dept;
begin
v_temp.deptno := 50;
v_temp.dname := 'aaaa';
v_temp.loc := 'bj';
dbms_output.put_line(v_temp.deptno || ' ' || v_temp.dname);
end;
--使用%rowtype声明record变量
[sql]
declare
v_temp dept%rowtype;
begin
v_temp.deptno := 50;
v_temp.dname := 'aaaa';
v_temp.loc := 'bj';
dbms_output.put_line(v_temp.deptno || ' ' || v_temp.dname);
end;
--PL/SQL里执行select语句的话,配合into(单个变量赋值),必须返回并只能返回一条记录
[sql]
declare
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
select ename,sal into v_ename,v_sal from emp where empno = 7369;
dbms_output.put_line(ename || ' ' || v_sal);
end;
declare
v_emp emp%rowtype;
begin
select * into v_emp from emp where empno = 7369;
dbms_output.put_line(v_emp.ename);
end;
declare
v_deptno dept.deptno%type := 50;
v_dname dept.dname%type := 'aaaa';
v_loc dept.loc%type := 'bj';
begin
insert into dept2 values(v_deptno,v_dname,v_loc);
commit;--别忘了!!!
end;
select * from dept2;
declare
v_deptno emp2.deptno%type := 10;
v_count number;
begin
--update emp2 set sal = sal/2 where deptno = v_deptno;
--select deptno into v_deptno from emp2 where empno = 7369;
select count(*) into v_count from emp2;
dbms_output.put_line(sql%rowcount || '条记录被影响');//sql,关键字,代表刚刚执行的sql程序
end;
--PL/SQL执行DDL语句,需要加execute immediate
[sql]
begin
execute immediate 'create table T(nnn varchar2(20) default ''aaaa'')';
end;
--if语句
[sql]
--取出7369的薪水,如果<1200,则输出'low',如果<2000则输出'middle',否则输出'high'
declare
v_sal emp.sal%type;
begin
select sal into v_sal from emp where empno = 7369;
if (v_sal<1200) then
dbms_output.put_line('low');
elsif(v_sal<2000) then --elsif没e
dbms_output.put_line('midddle');
else
dbms_output.put_line('high');
end if;
end;
--循环
[sql]
declare
i binary_integer := 1;
begin
loop
dbms_output.put_line(i);
i := i + 1;
exit when (i >= 11);
end loop;
end;
declare
j binary_integer := 1;
begin
while j < 11 loop
dbms_output.put_line(j);
j := j + 1;
end loop;
end;
begin
for k in 1..10 loop
dbms_output.put_line(k);
end loop;
for k in reverse 1..10 loop
dbms_output.put_line(k);
end loop;
end;
--错误处理
[sql]
declare
v_temp number(4);
begin
select empno into v_temp from emp where deptno = 10;
exception
when too_many_rows then
dbms_output.putline('太多记录了');
when others then
dbms_output.put_line('error');
end;
declare
v_temp number(4);
begin
select empno into v_temp from emp where empno = 2222;
exception
when no_data_found then
dbms_output.put_line('没有数据');
end;
--sql程序跨数据库平台较好,PL/SQL效率高
--DBA,错误日志
[sql]
create table errorlog
(
id number primary key,
errcode number,
errmsg varchar2(1024),
errdate date
);
create sequence seq_errorlog_id start with 1 increment by 1;
declare
v_deptno dept.deptno%type := 10;
v_errcode number;
v_errmsg varchar2(1024);
begin
delete from dept where deptno = v_deptno;
commit;
exception
when others then
rollback;
v_errcode := SQLCODE;
v_errmsg := SQLERRM;
insert into errorlog values (seq_errorlog_id.nextval,v_errcode,v_errmsg,sysdate);
commit;
end;
游标,触发器见下文