oracle PL、SQL(概念)

.PL/SQL简介。

  Oracle PL/SQL语言(Procedural Language/SQL)是结合了结构化查询Oracle自身过程控制为一体的强大语言,PL/SQL不但支持更多的数据类型,拥有自身的变量申明,赋值语句,而且还有条件,循环等流程控制语句。过程控制结构与SQL数据处理能力无缝的结合形成了强大的编程语言,可以创建过程和函数以及程序包。

二.优点:

1,支持SQL

    SQL是访问数据库的标准语言,通过SQL命令,用户可以操纵数据库的数据。

    PL/SQL支持所有的SQL数据操纵命令、游标控制命令、事务控制命令、SQL函数、运算符和伪列。

2,支持面向对象编程

    PL/SQL支持面向对象的编程,在PL/SQL中可以创建类型,可以对类型进行继承,可以在子程序中重载方法等。

3,更好的性能

    SQL是非过程语言,只能一条一条的执行,而PL/SQL把一个PL/SQL统一进行编译后执行,同时还可以把编译好的PL/SQL块存储起来,以备重用,减少了应用程序和服务器之间的通信时间,所以PL/SQL是高效而快速的。

4,可移植性

    使用PL/SQL编写的应用程序语言,可以移植到任何操作平台的ORACLE服务器,同时还可以编写可移植程序库,在不同环境中使用。

5,安全性

     可以通过存储过程对客户机和服务器之间的应用程序逻辑进行分割,这样可以限制对ORACLE数据库的访问,数据库还可以授权和撤销其他用户的访问权利。

 

  1 --1,PL/SQL--包含 过程、函数、触发器、...
  2      PL--> procedural  language  过程语言
  3  
  4 --查看错误信息能够更详细的指出错所在
  5 show error  
  6 
  7 ----1.1、第一个存储过程,简单定义(相当于一个静态方法)
  8 --(1)创建一个存储过程
  9 create  procedure  proc_holleword
 10 is
 11 begin
 12 dbms_output.put_line('Hello everyone !');  
 13 end;
 14 --(2)显示输出内容
 15 set serveroutput on;
 16 --(3)执行存储过程(二选一)
 17 execute procedurename;
 18 exec    procedurename;
 19 
 20 ---JAVA程序调用存储过程需要用call 
 21 
 22 ----1.2、第二个存储过程(简单案例)
 23 create or replace procedure proc_insertEmp
 24 is
 25 begin
 26  insert into emp values( 9901,'WANGQIANG' ,'CLERK');
 27  insert into emp values( 9902,'JIALIUL' ,'CLERK');
 28  insert into emp values( 9903,'ZHAOQI' ,'CLERK'); 
 29 end;
 30 
 31 --2,编写规范
 32 编写规范n
 33 2.1、注释;
 34 单行注释 --Sql 代码
 35 多行注释 /*...* /来划分
 36 
 37 2.2、标志符号的命名规范
 38 1).当定义变量时,建议用v_作为前缀v_sal
 39 2).当定义常量时,建议用c_作为前缀c_rate
 40 3).当定义游标时,建议用_cursor 作为后缀emp_cursor
 41 4).当定义例外时,建议用e_作为前缀e_error
 42 
 43 
 44 --3,代码块
 45 declare   --定义声明
 46 begin     --程序开始
 47 exception --异常  
 48 end;      ---程序结束
 49 
 50 
 51 ---- 案例 ---从对话框中输入一个员工编号,然后打印出该员工的姓名和工资
 52 declare
 53 v_ename varchar2(30);
 54 v_sal number(8,2);
 55 begin
 56 -- '&'开头的变量 是指从对话框输入值---
 57   select ename,sal into v_ename,v_sal from emp where empno=&eno;
 58   dbms_output.put_line('员工姓名:'||v_ename||'   工资:'||v_sal);
 59 end;
 60 /
 61 
 62 --4,exception 的使用案例,
 63 declare
 64 v_ename varchar2(30);
 65 v_sal  number(8,2);
 66 begin
 67   select ename,sal into v_ename,v_sal from emp where empno=&eno;
 68   dbms_output.put_line('员工姓名:'||v_ename||'   工资:'||v_sal);
 69 exception
 70   when no_data_found then
 71   dbms_output.put_line('该员工号不存在,请重新输入员工号');
 72 end;
 73 /
 74 
 75 
 76 --5,存储过程----带参数---   
 77 
 78 ----根据输入的员工编号,新的姓名,新的工资,去修改姓名和工资--------
 79 create procedure  proc_updateEmp(v_empno number,v_ename varchar2 ,v_sal number)
 80 is
 81 begin
 82 update emp set ename=v_ename  ,sal=v_sal where empno=v_empno;
 83 end;
 84 /
 85 ----执行--(传入参数)------------------
 86 exec  proc_updateEmp(9900,'LAOWANG',10000);
 87 
 88 
 89 
 90 --6,JAVA程序调用 存储过程 -------------------------------
 91 /* 
 92 建立一个Java程序
 93 加入oracle的驱动包ojdbc6.jar【app-administrator-product-11.2.0-dbhome_1-jdbc-lib-ojdbc6.jar】
 94 
 95 try {
 96             Class.forName("oracle.jdbc.OracleDriver");
 97             String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
 98             Connection conn=DriverManager.getConnection(url, "scott", "tiger");
 99 //调用格式
100             CallableStatement  cs=conn.prepareCall("{call proc_updateEmp(?,?,?)}");
101               cs.setInt(1, 9903);
102               cs.setString(2, "阿甘");
103               cs.setDouble(3, 9988.50);
104               cs.execute();
105               cs.close();
106               conn.close();
107         } catch (Exception e) {
108         }
109 */
110 
111 
112 
113 --7,条件分支语句-------------------------------------
114 if  ..  then   ..  end if;
115 if  ..  then   ..   else  ..  end if;
116 if  ..  then   ..   elsif .. then    ..  else .. end if ;
117 
118 
119 ----案例问题1:编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%。
120 create or replace procedure  proc_updateSalByEname(v_ename varchar2)
121 is
122 v_sal emp.sal%type;---取得emp表中的sal栏目的数据类型
123 begin
124   select sal into v_sal from emp where ename=v_ename;--根据名字查询
125   if v_sal<2000  then  
126     update emp set sal=sal*1.1 where ename=v_ename;
127    end if; 
128 end;
129 /
130 ----根据传入参数(名字)执行
131 exec proc_updateSalByEname('SMITH');
132 
133 
134 
135 ----案例问题2:编写一个过程,可以输入一个雇员名,如果该雇员的补助不是0 就在原来的基础上增加100;如果补助为0 就把补助设为200;
136 create or replace procedure  proc_updateCommByEname(v_ename varchar2)
137 is
138 v_comm emp.comm%type; --声明一个变量,并设置类型为emp中comm的类型
139 begin
140 --查询出comm后赋值给一个临时变量
141     select comm into v_comm   from emp where ename=v_ename;
142   if  nvl(v_comm,0) >0 then 
143     update emp set comm=comm+100 where ename =v_ename;  
144   else
145     update emp set comm=200  where  ename=v_ename;
146   end if;
147 end;
148 /
149 
150 
151 ----案例问题3:编写一个过程,可以输入一个雇员编号,如果该雇员的职位是PRESIDENT就给他的工资增加1000,如果该雇员的职位是MANAGER 就给他的工资增加500,其它职位的雇员工资增加200 
152 create or replace procedure  proc_updateSalByJob(v_ename varchar2)
153 is
154 v_job emp.job%type;
155 begin
156    select job into v_job from emp where ename=v_ename;
157   if  v_job='PRESIDENT' then 
158      update emp set  sal=sal+1000 where ename=v_ename;
159   elsif v_job='MANAGER'  then
160       update emp set  sal=sal+500 where ename=v_ename;
161   else 
162       update emp set  sal=sal+200 where ename=v_ename;
163   end if;
164 end;
165 /
166 
167 
168 --8,loop循环---------------------------------------
169 loop   ..  exit when ..  num+   end loop;
170 
171 
172 ----案例:建立一张stemp表 id sname  ,请插入100条数据
173 create table  stemp(id number(8),sname varchar2(20));
174 
175 declare
176 v_num number(8):=1; --声明变量并赋值【 :=  赋值符号】
177 begin 
178   loop 
179        insert into stemp values(v_num, 'sname'||v_num   );
180   exit when  v_num=100; --条件
181        v_num:=v_num+1;
182   end loop;--结束循环
183 end;
184 /
185 
186 --8.2、while循环--------------------------------
187 while  ..  loop ..........  num+  end loop;
188 
189 ----案例
190 declare
191 v_num number(8):=1;
192 begin 
193  while v_num<=100  loop 
194        insert into stemp values(v_num, 'sname'||v_num   );
195        v_num:=v_num+1;
196   end loop;
197 end;
198 /
199 
200 --8.3、for 循环--- reverse:反序-----------------
201 for v in 区间 loop   ...  end loop; 
202 for v in reverse 区间 loop  ...  end loop;
203 
204 ----案例
205 begin
206 for  v_num  in 1..1000  loop
207     insert into stemp values(v_num, 'sname'||v_num   );
208 end loop;
209 end;
210 /
211 
212 ----案例
213 begin
214 for  v_num  in  reverse 1..1000  loop
215     insert into stemp values(v_num, 'sname'||v_num   );
216 end loop;
217 end;
218 /

 

 

原文地址:https://www.cnblogs.com/bigerf/p/6489171.html