触发器使用

/* Source of PACKAGE BODY SYS.DBMS_OUTPUT is not available */
/
--定义变量 输出变量
declare
PI constant Pls_Integer:=3.1415655;
BEGIN
  dbms_output.put_line('PI=:'||PI);
END;




--两种动态类型:%type可以代表某列一致的类型, %rowtype可以代表与整条记录一致的集合类型
declare
e_name emp.ename%type;
e_mpinfo emp%rowtype;
--一般的复合类型(类似C语言的结构体)
type sal_comm is record(
      sal emp.sal%type,
      comm emp.comm%type
);
sc sal_comm;
type point is record(
     x pls_integer,
     y pls_integer
);
p point;
begin
select ename into e_name from emp where empno=7369;--赋值 用into
select * into e_mpinfo from emp where empno=7369;
dbms_output.put_line('姓名:'||e_name);
dbms_output.put_line('编号:'||e_mpinfo.empno);
--绑定数据 输出数据
select sal, comm into sc from emp where empno=7369;
dbms_output.put_line('7369''s sal:' || sc.sal || ' and comm is ' || nvl(sc.comm, 0));
p.x := 4;
p.y := 50;
dbms_output.put_line('p:' || p.x || ':' || p.y);
dbms_output.put_line(sc.sal);
end;


declare
-- v_deptno:=&no;                 --占位.弹对话框临时赋值,类似于C语音的scanf
--联合数组
type mytype is table
of emp%rowtype
index by varchar2(5);
emps mytype;
--嵌套表
type namestype is table 
of varchar2(10);
names namestype;
--可变数组
type daystype is varray(7) of varchar2(10);
days daystype:=daystype('Sunday','Monday');
begin
     --联合数组输出
     select * into emps('one') from emp where empno=7788;
     dbms_output.put_line('员工姓名:'||emps('one').ename);
     --嵌套表
      names := namestype('普京', '奥巴马', '雷锋');
     names.extend(4); --扩展一个 空间
     names(4) := '张三丰';
     dbms_output.put_line('第二位姓名是:'||names(2));
     --循环输出数据
     for i in 1..days.count loop
       dbms_output.put_line('days(' || i || '):' || days(i));
      end loop;
end;




--goto 示例
begin
  goto one;
  <<four>>
  for i in reverse 1..10 loop --reverse 反转数字 排序
    dbms_output.put_line('i:' || i);
    if i>8 then
      goto three;
    end if;
  end loop;
  <<two>>
  dbms_output.put_line('这是第二句话');
  goto four;
  <<three>>
  dbms_output.put_line('这是第三句话');
  goto "123";
  <<one>>
  dbms_output.put_line('这是第一句话');
  goto two;
  <<"123">>
  dbms_output.put_line('这是最后一句话');
end;


--异常绑定
declare 
isExists exception;
pragma exception_init(isExists,-1);
begin
       insert into emp(empno) values(7788);
       --捕获异常
      exception
       when isExists then
         dbms_output.put_line('主键不能重复');
end;
--1.查出人数最多的工资级别]


--2.查出重复的工资额度
 
--3.用PL/SQL打印出乘法表
begin
 for i in 1..9 loop
       for j in 1..i loop
        dbms_output.put(j||'*'||i||'='||j*i);
        dbms_output.put('  ');
        end loop; 
        dbms_output.new_line; --输出一点点空隙
    end loop;   
end;
select * from emp;
select * from dept;
select * from salgrade;
select * from bonus;
--创建触发器
create or replace trigger tr_emp_sal
before update on scott.emp for each row
begin
  if:new.sal<:old.sal then
  raise_application_error(-20010,'只能涨 不能降');
  end if;
end tr_emp_sal;
--测试触发器
update scott.emp set sal=100;
--数据字典        管理触发器
--查询已有的触发器
select * from user_triggers; 
--禁用触发器
alter trigger tirgger_name disable;
--激活触发器
alter trigger trigger_name enable;
--禁用或激活所有触发器
alter table table_name disable all triggers; 
alter table table_name enable all triggers; 
--删除触发器
drop trigger trigger_name;
--重新编译触发器
alter trigger trigger_name compile;


  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值