日期:2014-05-16  浏览次数:20400 次

创建触发器、函数、存储过程、执行语句
1、创建执行语句

declare 
       cursor venList is select * from tbl_venue ;
begin
       --for循环
       for ven in venList loop
          dbms_output.put_line('部门名称:'||VEN.id); 
      end loop;
      --COMMIT;
end ;

2、创建触发器
create or replace trigger 触发器名称
  after/before INSERT OR UPDATE OR DELETE on 表名
  for each row
declare
  mesg varchar2(100);
begin
  case
    when inserting then
      begin
        mesg := '赋值';
        用 :new来取新属性值,如: :new.id 
      EXception when others then 
        dbms_output.put_line('部门名称:' ||:old.id);
      end;
    when updating then
      begin
        mesg := '赋值';
        用 :new来取新属性值,如: :new.id 
        用 :old来取旧属性值,如: :old.id 
      EXception when others then 
        dbms_output.put_line('部门名称:' ||:old.id);
      end;
    when deleting then
      begin
        mesg := '赋值';
        用 :old来取旧属性值,如: :old.id 
      EXception when others then 
        dbms_output.put_line('部门名称:' || :old.id);
      end;
  end case;
end;

3、创建函数
(1)、单个属性
create or replace function getReportDate(param in varchar2 .. .)
  return varchar2 is --返回类型
  accept_time varchar2(1000);
  v_acc_nbr   date;
  
  CURSOR cur_1 is select sysdate from daul; --定义游标
begin
  open cur_1; --打开游标
  loop
    fetch cur_1
      into v_acc_nbr; --单个属性值
    exit when cur_1%notfound;
    if accept_time is null then
      ----执行语句
    else
      ----执行语句
    end if;
  end loop;
  close cur_1;
  return(accept_time); --返回
end getReportDate;

(2)、多个属性值
create or replace function getReportDate(param in varchar2 .. .)
  return varchar2 is --返回类型
  accept_time varchar2(1000);
  p1   date;
  p2   date;
  p3   date;
  
  CURSOR cur_1 is select sysdate,sysdate,sysdate from daul; --定义游标
begin
  open cur_1; --打开游标
  loop
    fetch cur_1  into p1,p2,p3; --多个属性值
    exit when cur_1%notfound;
    if accept_time is null then
      ----执行语句
    else
      ----执行语句
    end if;
  end loop;
  close cur_1;
  return(accept_time); --返回
end getReportDate;

4、创建存储过程
create or replace procedure 存储过程名称 is--无参
--create or replace procedure 存储过程名称(queryPara varchar2.. .) is --有参  
  sql_set varchar2(1024); --定义变量
  cursor crData is select * from 表名;
begin
  for rowData in crData loop
    --捕捉异常
    BEGIN 
      dbms_output.put_line('打印:' || rowData.id);
    EXCEPTION WHEN OTHERS THEN
        dbms_output.put_line('打印:');
    END;
  end loop;
end 存储过程名称;