oracl高级应用1

 1 高级查询、事务、过程、函数
 2 
 3 1. 内置函数。 abs() ——绝对值 ceil() —— 往上取值 floor() —— 往下取值 round()—— 四舍五入 trunc(数据,截取的位数) —— 正数表示小数点后面的位数。反之亦然。
 4 
 5 lower() upper() length() ltrim() rtrim() replace() substr(字符串,开始位置,截取长度)
 6 
 7 转换函数: to_number(字符串,字符串的格式) to_char() --时间类型(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')) to_date(字符串,字符串的格式)   (添加小时、分钟;比较时间大小等)
 8 
 9 nvl(字段名,默认值) —— 当字符值为null时,使用默认值填充。 nvl2(字段名,值1,值2) ——字符值不为null时,使用值1;为空,使用值2。
10 
11 2. 多表查询。 横向查询:inner joinleft joinright join 纵向查询(列数、数据类型相同):union(去掉重复数据) 、union all(所有,并集) intersect(交集——两张表都有的数据)、minus(补集——第一张减去第二张表的数据)
12 
13 --数据存放是有序存放。伪列rownum、rowid。 --Oracle的分页 select * from (select rownum r,e.* from scott.emp e) temp where temp.r>10 and temp.r<=15
14 
15 --备份表(或者表结构) create table temp2 as select * from scott.emp where 1=2
16 
17 3. 事务控制。 Oracle中默认所有的:insertupdate、delete都会启用事务控制。 (要求必须手动提交、回滚) 注意:需要撤销到指定位置,使用保存点。(savepoint 名字;) commit;--提交 rollback;--回滚(撤销)
18 
19 savepoint 名字; rollback to 保存点名。
20 
21 4. 存储过程。(数据库编程;定义方法()) SQLServer :TSQL编程 Oracle :PLSQL编程(适合大数据量,多表操作)面向对象编程。类、属性、方法 变量、方法(存储过程、函数)、条件分支,循环
22 
23 5. 自定义函数。 */
24 
25 --存储过程 --语法 create or replace procedure 过程名(参数1,参数2.....) as begin   --过程的内容 end;
26 
27 --两种方式调用过程: --1.命令行中。 exec 过程名; --2.SQL窗口调用。 /* begin     过程名; end ; */
28 
29 create or replace procedure proc_first as begin   dbms_output.put_line('Hell PLSQL!'); end;
30 
31 begin   proc_first; end; create table testTable as select * from scott.dept where 1=5
32 
33 create or replace procedure proc_second(dno number,dname varchar2,dloc varchar2,resultvalue out varchar2) as begin   insert into testTable values (dno,dname,dloc);    commit;   resultvalue := '恭喜你,操作ok鸟!';   --给变量赋值(:=赋值) end;
34 
35 declare    rs varchar2(50); begin   proc_second(2,'r','r',rs);   dbms_output.put_line('结果是:'||rs);-- + 加运算。 ||表示连接 end;
36 
37 --使用存储过程,添加数据 --伪类型(动态类型)  %type  %rowtype select dname,count(*) from scott.dept d,scott.emp e where e.deptno=d.deptno group by d.dname
38 
39 create or replace procedure proc_queryEmpInfo(eno number,dname1 out scott.dept%rowtype,sal_money out scott.emp%rowtype) as --定义变量 begin     --select dname into dname1 from scott.dept where deptno = (select deptno from scott.emp where empno=eno);     select d.* into dname1 from scott.dept d,scott.emp e where d.deptno = e.deptno and e.empno = eno;     select e.* into sal_money from scott.dept d,scott.emp e where d.deptno = e.deptno and e.empno = eno; end;
40 
41 declare     dname scott.dept%rowtype;     salm scott.emp%rowtype; begin   proc_queryEmpInfo(&请输入编号,dname,salm);   dbms_output.put_line('部门名:'||dname.deptno||':'||dname.dname);   dbms_output.put_line('薪水:'||salm.sal); end;
42 
43 --函数(跟过程基本类似,函数必须有一个返回值) --语法: create or replace function fun_test return varchar2 as --定义变量 begin   return 'abc'; end;
44 
45 select fun_test from dual;

 

posted @ 2013-01-17 23:40  全力以赴001  阅读(207)  评论(0编辑  收藏  举报