有2个表 他们是 组合关系 即 主子关系A
aid name
 1 特殊
 2 nullB
bid aid mingxi
 a 1 好
 b 1 强
 c 2 主要我现在想写 一段触发器
如果在 添加 修改前 mingxi = 主要 并且 aid=2 里 的name =null 就抛一个异常
不知道咋写create or replace trigger t_xxx
  before insert or update on B
  for each row
begin  if :new.mingxi = '主要' and :new.name is null --关键就是这个 and 不会写。。
  then
  raise_application_error(-222,'!!!出错了! 明细为 “主要” 的主表的Name为null了!');
  end if;
end t_xxx;

解决方案 »

  1.   

    Oracle 已经问了。我觉得 单纯讨论sql语句 都差不多把。
      

  2.   

    没写过oracle
      是不是跑错地方了
      

  3.   

    :new.name 这个肯定不对的啊
    new里面的东西都是B表里面的 
      

  4.   

    你可以通过 :new.aid查到A表里面的name放到一个变量里面 然后再判断
      

  5.   


    对!1Oracle 怎么接变量值  我给忘记 。。
      

  6.   

    类似这样declare   aname varchar2(32);
    select name into aname  from A where aid=:new.aid
      

  7.   


    create or replace trigger t_xxxx
      before insert or update   on B
      for each row
    declare dtzyx10 varchar2(20);
    begin
         select name into aname  from A where aid=:new.aid
      if :new.mingxi = '主要' and :aname  is null 
      then
      raise_application_error(-222,'!!!出错了! 明细为 “主要” 的主表的Name为null了!');
      end if;
    end t_xxx;?这样?
      

  8.   


    create or replace trigger t_xxxx
      before insert or update on B
      for each row
    declare aname varchar2(20);
    begin
      select name into aname from A where aid=:new.aid
      if :new.mingxi = '主要' and aname is null 
      then
      raise_application_error(-222,'!!!出错了! 明细为 “主要” 的主表的Name为null了!');
      end if;
    end t_xxx;
      

  9.   


    create or replace trigger t_xxxx
      before insert or update on B
      for each row
    declare aname varchar2(20);
    begin
      select name into aname from A where aid=:new.aid;
      if :new.mingxi = '主要' and aname is null 
      then
      raise_application_error(-222,'!!!出错了! 明细为 “主要” 的主表的Name为null了!');
      end if;
    end t_xxx;
      

  10.   


     select name into aname from A where aid=:new.aid;变量出错。。
      

  11.   


    create or replace trigger t_xxxx
      before insert or update on B
      for each row
    declare aname varchar2(64);
    begin
      select name into aname from A where aid=:new.aid;
      if :new.mingxi = '主要' and aname is null 
      then
      raise_application_error(-20200,'!!!出错了! 明细为 “主要” 的主表的Name为null了!');
      end if;
    end;怎么可能错 你的 name 是什么类型的
    自己加一个no_date_found的异常处理 
      

  12.   


    TRIGGER EEDZKY.T_XXX 编译错误错误:PLS-00049: 错误的赋值变量 'aname'
    行:7
    文本:if :new.mingxi = '1103A110000000003MHW' and :aname is null应该是Varchar2(200)把