我希望建立一个触发器,计算出其中(某列的字符串长度/60)的结果插入另一行。我的写法如下:CREATE OR REPLACE TRIGGER ECCDBA.MESSAGESEND_COUNT  
   before insert ON T1   --表名
   for each row
declare 
  M_count integer;       --结果变量
  M_content varchar(500);  --字符串长度变量
begin 
   M_content:=:new.MESSAGECONTEXT;  --先得到插入的字符串
   select length(M_content) into M_count from dual;  --计算出长度
   update  T_BASE_MESSAGESENDLIST set MESSAGECOUNT=(ceil(M_count/60)) where ID=:new.id;  --然后把(长度/60的 
                                                                                        结果更新到表中)
end btnlist_FOR_AUTOINC;结果我发现这样更新没有结果!!!!

解决方案 »

  1.   

    是对表A插入数据,然后更新表B的数据?
    确认表T_BASE_MESSAGESENDLIST里有对应数据?
      

  2.   

    update  T_BASE_MESSAGESENDLIST set MESSAGECOUNT=(ceil(M_count/60)) where ID=:new.id; 
    表T_BASE_MESSAGESENDLIST中,一定要有ID等於新插入的id值才能更新
      

  3.   

    对A表插入,然后更新A表的数据。T1那个我是为了方便你们看,所有让改成T1了。
    T1=T_BASE_MESSAGESENDLIST;
      

  4.   


    where ID=:new.id;  条件是否满足  T_BASE_MESSAGESENDLIST 中的ID中是否有T1中新插入的id
      

  5.   

    CREATE OR REPLACE TRIGGER ECCDBA.MESSAGESEND_COUNT  
       before insert ON T_BASE_MESSAGESENDLIST   --表名
       for each row
    declare 
    begin 
       :new.MESSAGECOUNT:=ceil(length(:new.MESSAGECONTEXT)/60);                                                                            
    end btnlist_FOR_AUTOINC;
      

  6.   

    这样做, 不然会引发变异表!CREATE OR REPLACE TRIGGER ECCDBA.MESSAGESEND_COUNT  
       before insert ON T_BASE_MESSAGESENDLIST   
       for each row
    begin 
       :new.MESSAGECOUNT:=(ceil(length(:new.MESSAGECONTEXT)/60)) ;  
    end btnlist_FOR_AUTOINC;
      

  7.   

    你的写法创建应该是不成功的你的意思就是更新表的时候把MESSAGECOUNT=(ceil(M_count/60))
    --这样CREATE OR REPLACE TRIGGER ECCDBA.MESSAGESEND_COUNT  
       before insert ON T1   
       for each row
    declare 
      M_count integer;       
      M_content varchar(500);  
    begin 
       M_content:=:new.MESSAGECONTEXT;  
       select length(M_content) into M_count from dual; 
       :new.MESSAGECOUNT:=(ceil(M_count/60)) ;
    end btnlist_FOR_AUTOINC;
      

  8.   


    CREATE OR REPLACE TRIGGER ECCDBA.MESSAGESEND_COUNT  
       before insert ON T_BASE_MESSAGESENDLIST  
       for each row
    declare 
      M_count integer;       
      M_content varchar(500);  
    begin 
       M_content:=:new.MESSAGECONTEXT;  
       select length(M_content) into M_count from dual; 
       :new.MESSAGECOUNT:=(ceil(M_count/60)) ;
    end btnlist_FOR_AUTOINC;
      

  9.   

    你的T_BASE_MESSAGESENDLIST里有和:new_id相同的记录吗?
    如果没有的话,肯定没有更新结果,其它的我不认为有错
      

  10.   

    这个好像能起作用,不过 :new.MESSAGECOUNT:=ceil(length(:new.MESSAGECONTEXT)/60);  的结果居然是0。
    我的MESSAGECONTEXT字段不应该是0 啊
      

  11.   

    结果是0的问题应该是:new.Messagecontext<60造成的吧,/ 出发应该结果是取的整数。59/60 = 0,不知道是不是因为这个呢,楼主可以试一下
      

  12.   

    楼主你忽悠人啊.
    SQL> create table T_BASE_MESSAGESENDLIST
      2  (id number(5),
      3  MESSAGECONTEXT varchar2(4000),
      4   MESSAGECOUNT   number(4))
      5  ;
     
    Table createdSQL> CREATE OR REPLACE TRIGGER MESSAGESEND_COUNT
      2     before insert ON T_BASE_MESSAGESENDLIST   --表名
      3     for each row
      4  declare
      5  begin
      6     :new.MESSAGECOUNT:=ceil(length(:new.MESSAGECONTEXT)/60);
      7  end btnlist_FOR_AUTOINC;
      8  /
     
    Trigger created
     
    SQL> insert into T_BASE_MESSAGESENDLIST(id,MESSAGECONTEXT)values(1,'1234');
     
    1 row inserted
     
    SQL> commit;
     
    Commit complete
     
    SQL> select * from T_BASE_MESSAGESENDLIST;
     
        ID MESSAGECONTEXT                                                                   MESSAGECOUNT
    ------ -------------------------------------------------------------------------------- ------------
         1 1234                                                                                        1
     
    SQL> 
      

  13.   

    length(:new.MESSAGECONTEXT)这样好像抓出来的值是0。
    MESSAGECONTEXT里面明明有值啊?
      

  14.   

    我的MESSAGECONTEXT类型是CLOB,这会不会有影响呢
      

  15.   

    哦,这样啊.试试这个CREATE OR REPLACE TRIGGER MESSAGESEND_COUNT
           before insert ON T_BASE_MESSAGESENDLIST   --表名
           for each row
        declare
        begin
           :new.MESSAGECOUNT:=ceil(DBMS_LOB.getlength(:new.MESSAGECONTEXT)/60);
        end btnlist_FOR_AUTOINC;
      

  16.   

    SQL> desc T_BASE_MESSAGESENDLIST;
    Name           Type      Nullable Default Comments 
    -------------- --------- -------- ------- -------- 
    ID             NUMBER(5) Y                         
    MESSAGECONTEXT CLOB      Y                         
    MESSAGECOUNT   NUMBER(4) Y                         
     
    SQL> insert into T_BASE_MESSAGESENDLIST values(1,'12345',111);
     
    1 row inserted
     
    SQL> select * from T_BASE_MESSAGESENDLIST;
     
        ID MESSAGECONTEXT                                                                   MESSAGECOUNT
    ------ -------------------------------------------------------------------------------- ------------
         1 12345                                                                                       1
     
    SQL> 
      

  17.   


    我找到原因了,我直接在toda里面通过手动输入就会认为MESSAGECONTEXT为0。通过语句就会正常识别!哎,真是奇怪啊