深度优先

这个家伙好懒,除了文章什么都没留下

0%

【Oracle】子程序(存储过程)和函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
select * from scott.emp

--1、什么是存储过程?
--预编译好的程序,简化应用程序的调用

--2、语法 (特殊的PL/SQL)
create procedure proc_test
as
begin
--sql语句
--exception
end;

--例1
--无参的存储过程
create procedure proc_sel4
as
myname varchar(10);
deptname varchar(10);
begin
--联表查询
select ename,dname into myname,deptname from scott.emp,scott.dept
where scott.emp.deptno = scott.dept.deptno and empno = 7369;
dbms_output.put_line(myname||deptname);
end;

--调用存储过程
begin
proc_sel4;
end;


select * from scott.emp;
select * from scott.dept;


--带参数的存储过程
create procedure proc_test(参数1,参数2....)
as
begin
--sql语句
end;

---加工资,manage岗位工资加?(参数1员工编号,参数2工资加多少)

create procedure proc_addsal1(myempno number,mysal number)
as
myjob varchar(10);
begin
select job into myjob from scott.emp where empno = myempno;
if myjob = 'MANAGER' then
update scott.emp set sal = sal+mysal where empno = myempno;
dbms_output.put_line('恭喜!你加了'||mysal||'工资');
else
dbms_output.put_line('sorry!没有加工资,赶紧往上爬!');
end if;
end;

--调用存储过程
begin
proc_addsal1(7698,1000);
end;

select * from scott.emp


create procedure proc_addsal3(myempno number,mysal number,jobtest varchar)
as
myjob varchar(10);
begin
select job into jobtest from scott.emp where empno = myempno;
if myjob = 'MANAGER' then
update scott.emp set sal = sal+mysal where empno = myempno;
dbms_output.put_line('恭喜!你加了'||mysal||'工资');
else
dbms_output.put_line('sorry!没有加工资,赶紧往上爬!');
end if;
end;

——————–我是分割线————————

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
--函数 = 方法
--预编译好的可以带返回值的操作

--语法
--无参的函数
create function fun_test1
return number
as
mysum number := 0;
begin
for i in 1..100
loop
mysum := mysum +i;
end loop;
return mysum;
end;

select fun_test1() from dual

--有参数的函数
create function fun_add(a number,b number)
return number
as
begin
return a+b;
end;

select * from scott.emp
--查询7499的用户的所有收入(sal+comm)
select empno,ename,(sal+comm) as 总收入 from scott.emp
select empno,ename,fun_add(sal,1000) as 总收入 from scott.emp