pl sql练习(2)

  1.尽可能了解oracle的功能,因为很多业务逻辑oracle已经为我们做了,比如oracle已经预定义了大量的异常代码,我们不必要写自己的异常而增加代码的复杂度。

例如oracle定义了当找不到符合要求的数据时定义的异常:no_data_found,而很多开发人员经常使用自己的逻辑去判断是否找到数据。

 declare
ln_deptno number;
ln_tempno number:=1;
begin
select deptno into ln_deptno
from scott.dept
where deptno=ln_tempno;
dbms_output.put_line(ln_deptno);
exception
when no_data_found then
dbms_output.put_line('No deptno for: '||ln_tempno);
end;
/ SQL> @a.sql;
No deptno for: 1 PL/SQL procedure successfully completed.

  2.管线表函数,可以把她的返回值作为像数据库表 一样在查询语句中使用。

 create or replace type color_hex_code as object
(
color_name varchar2(10),
color_code varchar2(10)
);
/
create or replace type color_type as table of color_hex_code;
/
create or replace function f_pipeline_test return color_type pipelined as
begin
pipe row(color_hex_code('black','#000000'));
pipe row(color_hex_code('red','#ff0000'));
pipe row(color_hex_code('green','#00ff00'));
pipe row(color_hex_code('blue','#0000ff'));
pipe row(color_hex_code('white','#ffffff'));
return;
end;
/ SQL> select * from table(f_pipeline_test); COLOR_NAME COLOR_CODE
---------- ----------
black #000000
red #ff0000
green #00ff00
blue #0000ff
white #ffffff

  3.传址函数的几种类型

 create or replace function adding
(a in number,b in out number)
return number is
begin
b :=b+1;
return a+b;
end;
/
variable one number;
/
variable two number;
/
begin
:one :=1;
:two :=0;
end;
/
variable output number;
/
call adding(:one,:two) into :output;
/
call adding(:one,:two) into :output;
/ select :one, :two from dual; :ONE :TWO
---------- ----------
1 4

  4.使用rollup对分组数据进行汇总

 select deptno,sum(sal) from emp where deptno is not null
group by rollup(deptno); DEPTNO SUM(SAL)
---------- ----------
10 8750
20 10875
30 9400
29025

  5.使用cube对分组内每一组的数据和总数据同时汇总,同时包含对不同分组依据进行汇总

 select deptno,job,sum(sal) from emp where deptno is not null
group by cube(deptno,job)
order by deptno,job; DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 8750
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
20 10875
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
30 9400
ANALYST 6000
CLERK 4150
MANAGER 8275
PRESIDENT 5000
SALESMAN 5600
29025 18 rows selected.

  6.使用grouping sets 只显示汇总,不列出原始分组数据

 SELECT deptno,job,SUM(sal)
FROM emp
WHERE deptno IS NOT NULL
GROUP BY GROUPING SETS(deptno,job)
ORDER BY deptno,job; DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 8750
20 10875
30 9400
ANALYST 6000
CLERK 4150
MANAGER 8275
PRESIDENT 5000
SALESMAN 5600

  7.grouping_id函数可以用来标记汇总数据的行,比如第一个维度的汇总用1表示,第二个维度的汇总用2表示,两个维度的所以汇总用3表示。因此可以过滤未汇总的行。

 ELECT deptno,job,
GROUPING_ID(deptno,job),SUM(sal)
FROM emp
WHERE deptno IS NOT NULL
GROUP BY CUBE(deptno,job)
HAVING GROUPING_ID(deptno,job) > 0
ORDER BY deptno,job; DEPTNO JOB GROUPING_ID(DEPTNO,JOB) SUM(SAL)
---------- --------- ----------------------- ----------
10 1 8750
20 1 10875
30 1 9400
ANALYST 2 6000
CLERK 2 4150
MANAGER 2 8275
PRESIDENT 2 5000
SALESMAN 2 5600
3 29025

  8.利用子查询克隆表结构

 CREATE TABLE emp2
AS
SELECT *
FROM scott.emp
WHERE 1=2; SQL> SELECT * FROM emp2; no rows selected

  9.用户查看自身表的使用情况(user_extents)

  SELECT segment_name,segment_type,
tablespace_name,
bytes/1024/1024 "size",
blocks from user_extents; SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME size BLOCKS
-------------------- ------------------ ------------------------------ ---------- ----------
ORDERS TABLE USERS .0625 8
EMP TABLE USERS .0625 8
EMP2 TABLE USERS .0625 8

  10.用户查看自身表的使用情况(user_segments)

 SELECT segment_name,segment_type,tablespace_name,
bytes/1024/1024 byt,buffer_pool
FROM user_segments; SEGMENT_NAME SEGMENT_TYPE TABLESPACE_NAME BYT BUFFER_
---------------------------------------- ------------------ ------------------- ---------- -------
EMP2 TABLE USERS .0625 DEFAULT
EMP TABLE USERS .0625 DEFAULT
ORDERS TABLE USERS .0625 DEFAULT
BIN$iZfhhmba19rgQKjAZQETvA==$0 TABLE USERS .0625 DEFAULT
BIN$iZfhhmbZ19rgQKjAZQETvA==$0 TABLE USERS .0625 DEFAULT
BIN$iZfhhmbY19rgQKjAZQETvA==$0 TABLE USERS .0625 DEFAULT
上一篇:身处IT的你对身边人都有哪些影响


下一篇:chrom 快捷键 整理版