|
- 创建一个添加FOOD的存储过程
1
2
3
4
5
6
7
8
9
10
| create or replace procedure add_food_pro (name in varchar,price in number,description in varchar)
as
begin
insert into food (f_name,f_price,description)values(name,price,description);
commit;
end;
--下面的代码是调用存储过程
begin
add_food_pro('糖醋鱼',12,'美味');
end;
|
创建一个带有输出参数的存储过程,以a+b=c为例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| create or replace procedure add_num_pro (a in int,b in int,c out int)
as
begin
c:=a+b;
end;
--接下来调用存储过程
declare
c int;
begin
add_num_pro(10,20,c);
dbms_output.put_line(c);
end;
--注意最后的dbms_output.put_line();
--如果想要看到结果,需要在打开控制台,set serveroutput on
|
oracle数据库中游标的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| declare
cursor c is select *from employees;
hang employees%rowtype;
begin
--打开游标
open c;
loop
fetch c into hang;
dbms_output.put_line('该员工的姓名是'||hang.first_name||',工资是'||hang.salary);
exit when c%notfound;
end loop;
--关闭游标
if c%isopen then close c;
end if;
end;
|
创建一个函数
1
2
3
4
5
6
7
8
9
| --创建一个函数,可以去掉字符串的空格
create or replace function noblank1( str varchar)
return varchar
is
begin
return replace(str,' ','');
end;
--调用函数
select noblank1(' dfa d s a ') from dual;
|
创建一个触发器
1
2
3
4
5
6
7
8
9
| --创建一个触发器
create or replace trigger stu_update_tri
before update on student
for each row
begin
dbms_output.put_line('某条记录已经更新,原来的数据是'||:old.age||'新的数据是'||:new.age);
end;
--触发触发器
update student set age=21;
|
|
|