第一个表TBInfo中有三个字段MasterId char(18),DTime char(16)
第二个表TBCount中有两个字段MasterId char(18),DTime char(16) , DCount char(1)
当像TBCount表中插入数据时触发比如 Insert Into TBInfo (MasterId,DTime) Values ('13065019861126001x',‘2009120211260000’)   <DTime是精确到毫秒的时间>
1.首先判断TBCount表中今天是否有这条数据:Select Count(*) From TBCount Where MasterId='13065019861126001x' And DTime>'今天的零时,像<2009120200000000>' 
1.如果有0条记录则将这条记录插入TBCount表中,并把DCount字段赋为1;
2.如果有记录并且记录数小于2则Update把DCount字段++1;
3.如果有记录并且记录数大于等于2,则抛出异常RAISE_APPLICATION_ERROR(-20055,'INVALID   CODE')阻止其插入; 
这个触发器实现的逻辑就是在一个MasterId在一天之内只能插入两条数据
我一直用MSSql的,对Oracle的语法不太懂,哪位大侠帮帮忙啊分不是问题

解决方案 »

  1.   

    第一个表TBInfo中有三个字段MasterId char(18),DTime char(16) 
    第二个表TBCount中有两个字段MasterId char(18),DTime char(16) , DCount char(1) 
    当像TBCount表中插入数据时触发比如 Insert Into TBInfo (MasterId,DTime) Values 
    描述的有问题吧……
      

  2.   

    这样的话tbcount里dtime的时间是当天第一次插入的时间?create or replace trigger tg_tbinfo
    before insert on tbinfo
    for each row
    declare 
    v char(1);
    begin
    select max(dcount) into v from tbcount where masterid=:new.masterid and dtime>to_char(trunc(sysdate),'yyyymmddhh24miss')||'00';
    if v is null then 
      insert into tbcount values(:new.masterid,:new.dtime,'1');
    elsif v='1' then
      update tbcount set dcount='2' where masterid=:new.masterid and substr(dtime,1,8)=to_char(sysdate,'yyyymmdd');
    else
      RAISE_APPLICATION_ERROR(-20055,'INVALID CODE');
    end if;
    end;