pl/sql 实例精解 03

1. 在Pl/sql 中使用 sql

   1:  /*
   2:   * 一个 pl/sql 语句块, 只是一个容器, 是表明一个整体的容器, 容器里可以放置多个sql语句
   3:  */
   4:   
   5:  declare 
   6:      v_zip    zipcode.zip%type;
   7:      v_user    zipcode.created_by%type;
   8:      v_date    zipcode.created_date%type;
   9:  begin
  10:      -- statement 1
  11:      select 43438, user, sysdate
  12:        into v_zip, v_user, v_date
  13:        from dual;
  14:      
  15:      -- statement 2
  16:      insert into zipcode(zip, created_by, careate_date, modified_by,
  17:                          modified_date)
  18:      values(v_zip, v_user, v_date, v_user,
  19:              v_date);
  20:  end;
  21:  /
  22:  show errors;
  23:      

2. 在 pl/sql 中使用 commit, rollback, savepoint

   1:  /*
   2:   * ROLLBACK [WORK] to SAVEPOINT name;
   3:  */
   4:   
   5:  begin
   6:      insert into student(student_id, last_name, zip, reistration_date,
   7:                          created_by, created_date, modified_by, modified_date)
   8:      values(student_id_seql.nextval, 'Tashi', 10015, '01-一月-99',
   9:              'Student', '01-一月-99', 'studenta', '01-一月-99');
  10:      savepoint A;        -- A is work name
  11:      
  12:      insert into student(student_id, last_name, zip, reistration_date,
  13:                          created_by, created_date, modified_by, modified_date)
  14:      values(student_id_seql.nextval, 'bbb', 10015, '01-一月-99',
  15:              'Student', '01-一月-99', 'studentb', '01-一月-99');
  16:      savepoint B;        -- B is work name
  17:      
  18:      insert into student(student_id, last_name, zip, reistration_date,
  19:                          created_by, created_date, modified_by, modified_date)
  20:      values(student_id_seql.nextval, 'ccc', 10015, '01-一月-99',
  21:              'Student', '01-一月-99', 'studentc', '01-一月-99');
  22:      savepoint C;        -- C is work name
  23:      
  24:      rollback to b;
  25:  end;

如上代码执行完后, 由于 rollback to b, 所以, 第3段代码执行的插入操作就会被rollback, 所以最后的结果是插入了2条记录.

所以, 综上, rollback 到之前的一个保存点 savepoint, 那么该保存点之下的所有sql语句都会被rollback, 而保存点savepoint之上的sql没有被rollback.

原文地址:https://www.cnblogs.com/moveofgod/p/3651898.html