您的位置:首页 > 数据库 > Oracle

Oracle的PL/SQL语句介绍

2013-03-18 20:53 591 查看
pl/sql 三个组成部分:(1)变量声明部分(2)可执行部分 (3)异常处理部分

(1)变量声明部分

       a 、 首先必须给予变量正确的名称(命名规范)

       注意与SQL SERVER区分:在SQL SERVER 中自定义的变量命名以@开头

       b 、赋予变量正确的数据类型

       能赋予的数据类型:oracle自带的数据类型(varchar2(n),number(p,s),date)

                         oracle支持的数据类型(integer, string )

                         表名.字段名%type,表名%rowtype

                         记录类型(record) type record_type  is record(id number(10),name varchar2(20));  

                         异常变量类型 (exception)    

       c、在声明部分给变量赋予初值的两种方式: 1、:=   2、default     

       d、常量的使用(注意声明常量的时候就必须赋予初值,常量的值不能更改的)

       

 (2)可执行部分

       a、增、删、改语句以及"select..into .. from .."可以直接放置在begin..end之间

       b、在可执行部分给变量赋予初值的两种方式: 1、 :=  2、select..into .. from ..

       c、dbms_output.put_line() 以及 dbms_output.put() 还有dbms_output.new_line之间的关联以及区别 

       d、流程控制语句 

          if 分支

             if ... then

             ..

             elsif .. then --注意elsif

             ...

             else 

             ...

             end if;

             --并列if

             if .. then

                ..

             end if;

             if .. then

                ..

             end if;

             

             --嵌套if

              if .. then

                 if .. then

                    ...

                 end if;

             end if;

             

             

          case 分支

          case

               when .. then ..;

               when .. then ..;

               else

               ...

          end case;

          

          --基本循环

          loop

              --退出循环的两种写法

              if ... then

                 exit;--退出 

              end if;

              

              --第二种写法

              exit when 条件;

          end loop;

          

          --while循环

          while 条件

          loop

          ...

          end loop;

       

         --for循环

         for x in [reverse] value1..value2

         loop

             .....

             --x := x+1; 错误的

         end loop;

         --注意:x是不需要声明的  value1的值一般都是小于value2,如果反向采用reverse关键字

                --x的值不能够显示的修改

                

                

        --goto语句 以及 null语句

        

--null语句最大的一个好处就是维护我们正常的语法规则

declare

        x number;

        y varchar2(20);

begin

        x:=3;

        case 

            when x=1 then y:=1;

            when x=2 then y:=2;

            when x=3 then null;

            else

                 y:=4;

        end case;

        dbms_output.put_line(y);

end;

       

       e、动态SQL语句

       语法:

       execute immediate '动态SQL语句'

       [into ...]

       [using ...]

       

begin

       create table test(tno number,tname varchar2(20));

end;

       

       f、使用自定义的记录数据类型

       

(3)异常处理部分

     a、预定义异常

        exception

              when 异常名称 then

                   异常处理;

                   

     b、自定义的异常

        声明异常变量 e exception;

        自己定义规则,决定什么时候抛出异常 raise e;

        捕获异常

             exception

              when 异常名称 then

                   异常处理; 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息