PL/SQL基础

PL/SQL是过程化的语言,面向过程,关注细节,可以实现复杂的业务逻辑。

1.PL/SQL优势

    ①提高程序的运行性能

    ②程序模块化

    ③可逻辑控制语句的执行

    ④使用exception可以获得并处理语句异常

    ⑤移植性强,成功移植到不同版本的ORACLE中。

2.PL/SQL结构

    declare --非必须

    begin

    exception --非必须

    end;

其中begin ... end;是语句的执行部分,必须有内容。

注:如果在SQL PLUSE中执行PL/SQL需要在前输入set serverputout on回车数据语句即可,这样保证输出会在控制天显示。

注:select ...into...是oracle中的赋值语句,但每次只返回一条记录,多条记录会出现异常。x into y:将查询出来的x赋值给y。

注释分为两种,

    第一种:--单行注释

    第二种:/*

                 XXXXXXXXXXXXXXX注释/*...*/整个内容。

      */

3.PL/SQL变量

    变量:

    n_count number;

    常量:

    n_count constant number;

基础变量分类:

    数值类型:

    number:整数浮点数

    pls_integer:溢出发生异常,整数

    binary_integer:溢出不发生异常,证书

    simple_integer:不发生异常使用此类型,性能较pls_integer高。

   字符类型:char、varchar2,nchar、nchar2、long:变长,最大存2GB。

    日期类型:date、timestamp

    %type:与列名相同的类型,

符合类型变量

4.控制结构

    4.1 if条件

    if...\if ...else...\if...elsif...else...

    具体语法

declare
  n_count number := 2;
begin
  if n_count > 1 then
    dbms_output.put_line(n_count);
  end if;
end;

 

    4.2 case条件

    简单case、搜索式case

declare--简单case
  n_count number := 2;
begin
  case n_count
    when 1 then
      dbms_output.put_line(n_count);
    when 2 then
      dbms_output.put_line('OK');
    else
      dbms_output.put_line('ERROR');
  end case;
end;

  

declare--搜索式case
  n_count number := 2;
begin
  case 
    when n_count=1 then
      dbms_output.put_line(n_count);
    when n_count=2 then
      dbms_output.put_line('OK');
    else
      dbms_output.put_line('ERROR');
  end case;
end;

     4.3 基本loop

declare
  --简单loop
  n_count number := 0;
begin
  <<basic_loop>>--标签:可不写
  loop
    dbms_output.put_line('当前n_count的值:' || n_count);
    n_count := n_count + 1;
    if n_count = 3 then
      dbms_output.put_line('退出->当前n_count的值:' || n_count);
      exit basic_loop;--如果定义标签,在这可以跳出
    end if;
  end loop;
end;
-------
declare
  --exit when类型loop
  n_count number := 0;
begin
  <<basic_loop>>
  loop
    dbms_output.put_line('当前n_count的值:' || n_count);
    n_count := n_count + 1;
    exit basic_loop when n_count = 3;--先执行exit - when 语句
  end loop;
  dbms_output.put_line('退出->当前n_count的值:' || n_count);
end;

      4.4 while loop语句

declare
  --while类型loop,先判断后执行
  n_count number := 0;
begin
  while n_count < 3 loop
    dbms_output.put_line('当前n_count的值:' || n_count);
    n_count := n_count + 1;
  end loop;
  dbms_output.put_line('退出->当前n_count的值:' || n_count);
end;
---------
declare
  --while类型loop,先执行后判断
  n_count number := 0;
  flag    boolean := true;
begin
  while flag loop
    dbms_output.put_line('当前n_count的值:' || n_count);
    n_count := n_count + 1;
    if (n_count = 3) then
      flag := false;
    end if;
  end loop;
  dbms_output.put_line('退出->当前n_count的值:' || n_count);
end;

      4.5 for loop语句

declare
  --for类型loop
  n_count number := 0;
begin
  for inx in 1 .. 3 loop  --1:代表下界 3:代表上界
    dbms_output.put_line('当前n_count的值:' || n_count);
    n_count := n_count + 1;
  end loop;
  dbms_output.put_line('退出->当前n_count的值:' || n_count);
end;

       4.6 ddl/dml

declare
  --ddl
  v_sql varchar2(200);
begin
  v_sql := 'create table test_1238 (id number)';
  execute immediate v_sql;
end;
-------
declare
  --dml
  v_sql varchar2(200);
begin
  insert into test_1238 values (32);
  commit;
  v_sql := 'insert into test_1238 values (34)';
  execute immediate v_sql;
  commit;
end;

注意:在PL/SQL中select语句的使用为 select 1 into vals from dual;

           执行DDL语句中,需要 execute immediate ‘语句’,关于 execute immediate的使用将在后续推出。

posted @ 2018-07-05 19:52  i孤独行者  阅读(926)  评论(1编辑  收藏  举报