1、存储过程小例子
create or replace procedure test_pro(workDate out varchar2) is
begin
select sysdate into workDate from dual;
end test_pro;
执行:
SQL> set serveroutput on
SQL> declare
2 workDate varchar2(20);
3 begin
4 test_pro(workDate);
5 dbms_output.put_line(workDate);
6 end;
7 /
2、for ... in ... loop
end loop;
e.g
for i in 1..100 loop // i从1开始,100结束
end loop;
eg
DECLARE
i number:=0;
begin
for i in 1..1002 loop
do....
commit;
end loop;
end;
3、oracle特有的decode()函数
decode(parameter,if1,then1,if2,then2,...,else);
如果 参数值为if1,则decode函数返回值为then1...,都不满足则为else;
e.g
USERID
NAME
SEX
AGE
BIRTHDAY
HOBBY
ADDRESS
98
张三
男
0
2015/6/19 17:06:21
上网,上网,逛街
南京
(1).select decode(userid,98,9998,1000) from user_test where userid=90;
结果:
DECODE(USERID,98,9998,1000)
1000
(2).select decode(userid,98,9998,1000) from user_test where userid=98;
结果:
DECODE(USERID,98,9998,1000)
9998
4、分层查询[where condition] start with condition connect by prior condition
e.g
create table test_s(emp_no number,mgr number);
insert into test_s values(100,10);
insert into test_s values(101,20);
insert into test_s values(103,40);
insert into test_s values(104,40);
insert into test_s values(105,10);
insert into test_s values(106,20);
insert into test_s values(10,1);
insert into test_s values(20,2);
insert into test_s values(40,4);
insert into test_s values(1,0);
insert into test_s values(2,0);
insert into test_s values(4,0);
commit;
树形图如下:
0
/ | \
1 2 4
| | |
10 20 40
/ \ / \ / \
100 105 101 106 103 103
(1).select * from test_s start with emp_no=2 connect by prior emp_no=mgr;
查询结果:
EMP_NO
MGR
2
0
20
2
101
20
106
20
改变prior的位置时
(2).select * from test_s start with emp_no=2 connect by emp_no= prior mgr;
EMP_NO
MGR
2
0
PRIOR被置于CONNECT BY子句中等号的前面时,
由父节点向子节点方向通过树结构,
我们称之为自顶向下的方式。
PIROR运算符被置于CONNECT BY 子句中等号的后面时,
由子节点向父节点方向通过树结构,
我们称之为自底向上的方式。