)
100;
dbms_output.put_line(v_sal);
exception
when too_many_rows then dbms_output.put_line('
输出的行数过多');
when others then dbms_output.put_line('出现其它的异常了');
end;
rush:
sql;">
declare
e_deleteid_exception exception;
pragma exception_init(e_deleteid_exception,-2292);
begin
delete from employees
where employee_id = 100;
exception
when e_deleteid_exception then dbms_output.put_line('违反了完整性约束,故不能
删除此
用户');
when others then dbms_output.put_line('出现其它的异常了');
end;
10000 then raise e_sal_hight;
end if;
exception
when e_sal_hight then dbms_output.put_line('工资太高了');
when others then dbms_output.put_line('出现其它的异常了');
end;
通过select...into...查询某人的工资,若没找到则打印出“未找到此数据”:
rush:
sql;">
declare
v_sal employees.salary%type;
begin
select salary into v_sal from employees where employee_id = 1001;
exception
when no_data_found then dbms_output.put_line('未找到此数据');
when others then dbms_output.put_line('出现其它的异常了');
end;
更新指定员工工资,如工资小于300,则加100,对NO_DATA_FOUND异常,TOO_MANY_ROWS进行处理。
declare
v_sal employees.salary%type;
begin
select salary into v_sal from employees where employee_id = 1001;
if v_sal < 300 then update employees set salary = salary + 100 where employee_id =101;
end if;
exception
when no_data_found then dbms_output.put_line('未找到此数据');
when too_many_rows then dbms_output.put_line('
输出的行数太多了');
when others then dbms_output.put_line('出现其它的异常了');
end;
自定义异常:
更新指定员工工资,增加100;若指定员工不在,则抛出异常:NO_RESULT;
rush:
sql;">
declare
no_result exception;
begin
update employees set salary = salary + 100 where employee_id = 1001;
if
sql%notfound then raise no_result;
end if;
exception
when no_result then dbms_output.put_line('查无此数据,更新失败');
when others then dbms_output.put_line('出现其它异常');
end;
写个简单的hello_world存储函数
rush:
sql;">
create or replace function hello_world
return varchar2
is (相当于declare,可以在其后面定义变量、记录、游标)
begin
return 'helloworld';
end;
存储
函数的
调用:
begin
dbms_output.put_line(hello_world);
end;
或者:
select hello_world from dual;
的存储函数:
rush:
sql;">
create or replace function hello_world1(v_
logo varchar2)
return varchar2
is
begin
return 'helloworld'||v_
logo;
end;
调用:
select hello_world1('shellway') from dual
或者:
begin
dbms_output.put_line(hello_world1('shellway'));
end;
定义一个获取系统时间的函数:
rush:
sql;">
create or replace function get_sysdate
return varchar2
is
begin
return to_char(sysdate,'yyyy-MM-dd HH24:mi:ss');
end;
函数,两个数相加
rush:
sql;">
create or replace function add_p
aram(v_num1 number,v_num2 number)
return number
is
v_num3 number(10);
begin
v_num3 := v_num1 + v_num2;
return v_num3;
end;
调用:
select add_p
aram(2,5) from dual;
或者:
begin
dbms_output.put_line(add_p
aram(5,4));
end;
定义一个函数:获取给定部门的工资总和,要求:部门号定义为参数,工资总额为返回值:
rush:
sql;">
create or replace function get_sal(dept_id number)
return number
is
v_sumsal number(10) := 0;
cursor salary_cursor is select salary from employees where department_id = dept_id;
begin
for c in salary_cursor loop
v_sumsal := v_sumsal + c.salary;
end loop;
return v_sumsal;
end;
调用:
select get_sal(80) from dual;
一个函数:获取给定部门的工资总和 和 该部门的员工总数(定义为OUT
类型的参数)。
要求:部门号定义为参数,工资总额定义为返回值。
rush:
sql;">
create or replace function get_sal(dept_id number,total_count out number)
return number
is
v_sumsal number(10) := 0;
cursor salary_cursor is select salary from employees where department_id = dept_id;
begin
total_count := 0;
for c in salary_cursor loop
v_sumsal := v_sumsal + c.salary;
total_count := total_count + 1;
end loop;
return v_sumsal;
end;
调用:
declare
v_count number(4);
begin
dbms_output.put_line(get_sal(80,v_count));
dbms_output.put_line(v_count);
end;
一个存储过程:
获取给定部门的工资总和(通过out参数),要求部门号和工资总额定义为参数。
(注意:存储过程和存储函数是不一样的,存储函数有返回值而存储过程没有,调用时候存储过程直接调用)
rush:
sql;">
create or replace procedure get_sal1(dept_id number,sumsal out number)
is
cursor salary_cursor is select salary from employees where department_id = dept_id;
begin
sumsal := 0;
for c in salary_cursor loop
sumsal := sumsal + c.salary;
end loop;
dbms_output.put_line(sumsal);
end;
调用:
declare
v_sal number(10):=0;
begin
get_sal1(80,v_sal);
end;
对给定部门(作为输入参数)的员工进行加薪操作,若其到公司的时间在(?,95)期间,为其加薪5%
(95,98) 3%
(98,?) 1%
得到以下返回结果:为此次加薪公司每月额外付出多少成三(定义一个OUT型的输出参数)
rush:
sql;">
create or replace procedure add_sal(dept_id number,temp out number)
is
cursor sal_cursor is select employee_id,salary,hire_date
from employees where department_id = dept_id;
v_temp number(4,2):=0;
begin
temp := 0;
for c in sal_cursor loop
if to_char(c.hire_date,'yyyy') < '1995' then v_temp:=0.05;
elsif to_char(c.hire_date,'yyyy') < '1998' then v_temp:=0.03;
else v_temp:=0.01;
end if;
update employees
set salary = salary * (1+v_temp)
where employee_id = c.employee_id;
temp := temp + c.salary*v_temp;
end loop;
dbms_output.put_line(temp);
end;
调用:
declare
v_i number(10):=0;
begin
add_sal(80,v_i);
end;
:在INSERT,UPDATE,DELETE情况下会触发TRIGGER
该TRIGGER是在触发事件发生之前(BEFORE)还是之后(AFTER)
该TRIGGER被触发之后的目的和意图,正是触发器本身要做的事情,如PL/sql块
有语句级(STATEMENT)触发器和行级(ROW)触发器
写一个简单的触发器:
rush:
sql;">
create or replace trigger update_emp_trigger
after
update on employees
for each row (行级触发器,即每更新一条记录就会
输出一次'helloworld',若没有这语句则是语句级触发器)
begin
dbms_output.put_line('helloworld');
end;
rush:plain;">
1、
create table emp1
as
select employee_id,email from employees where department_id = 80;
2、
create or replace trigger update_emp_trigger2
after
update on emp1
for each row
begin
dbms_output.put_line('old salary:'||:old.salary||'new salary:'||:new.salary);
end;
3、
update emp1 set salary = salary + 100 ;
编写一个触发器,在对记录进行删除的时候,在表中备份对应的记录
rush:plain;">
1、创建my_emp表:
create table my_emp
as
select employee_id,salary from employees ;
2、创建my_emp_bak表:
create table my_emp_bak
as
select employee_id,salary from employees where 1=2;
3、检查创建的表中的记录:
select * from my_emp
select * from my_emp_bak
4、创建
一个触发器:
create or replace trigger delete_emp_trigger
before
delete on my_emp
for each row
begin
insert into my_emp_bak
values(:old.employee_id,:old.salary);
end;
5、执行含有触发器时
间的语句:
delete from my_emp
6、检查触发器执行后的结果:
select * from my_emp
select * from my_emp_bak