在SQL表中可以使用自定義函數﹕
 
表A﹕ a ,b  兩個字段。 
當INSERT INTO A(b) values(200) 時。 a 的值會0.15*200;
每插入一條數據b,就自動產生a=b*0.15的數據。   
 
    而在Oracle中也可以實現這個功能嗎?  寫一個自定義函數?怎么調用呢?
SQL中有公式﹐ORACLE怎么做呢?
不知道從何下手﹐請大俠 引導。                                

解决方案 »

  1.   


    INSERT INTO A(b) values(200*0.15);   -- 如果输入的参数值是200
    INSERT INTO A(b) values(650*0.15);   -- 如果输入的参数值是650
      

  2.   

    应该是inert into a(a,b) values(200*1.5,200)
      

  3.   

    create or replace function f_test(v_intb in number) return number is
    begin
      return (v_intb * 0.15);
    end;
    /
    编译成功后,如下调用:
    select f_test(200) from dual;
      

  4.   

    tableA中有 a ,b 兩個字段,使用:
    INSERT INTO tableA values(f_test(200),200);
      

  5.   

    同样的 问题在SQL中建立自定义函数.我可以实现,但是在ORACLE中我就不知道是怎么做的了,以上的语句我都会,但是不是我想要的答案!
      

  6.   

    什么意思。是不是自动生成a的数据阿。比如插入一条B=200,自动的a=0.15*200阿要用触发器实现阿。
    create or replace trigger tr_a
      before update or insert  of B on a
      for each row
    begin
      update a set a = b * 0.15;
    end;
      

  7.   

    ONLYRAO, 你举个例子:源数据+相应的数据 我们就明白了。
      

  8.   


    mantisXF 我知道你經常就在我背后﹐幫我也有那么多次了。這次你可不能袖手旁觀哦﹗
      

  9.   

    --针对你私下提出的需求,给你大致写了个:CREATE OR REPLACE procedure onlyrao(mh1 IN VARCHAR2,djb1 in varchar2,m1 in varchar2,y in varchar2,kb1 varchar2) as 
    ZDBJ1 varchar2(20); 
    JD_BFB1 number:=1; 
    JDFY1 varchar2(20):='计算公式'; 
    begin
    --可在此进行公式的详细赋值计算 
    insert into cost values(MH1,DBJ1,m1,Y1,KB1,zdbj1,jd_bfb1,jdfy1); 
    end; 
    / --编译如成功后,再如下执行: exe onlyrao('HS0811001','5000','11','2008','鉗工一課'); --再查看一下结果: 
    select * from cost where mh='HS0811001'; 
      

  10.   


    --此表为操作表:
    create table cost0(
    MH VARCHAR2(30),  -- 主键,模號          
    DBJ Number(10,2), --得標價              
    KB CHAR(10), --課別                     
    Y Number (10), --年                     
    M Number (10), --月                     
    constraint cost0_pk primary key (mh));--此表为数据查看表:
    create table cost(
    --MH VARCHAR2(30),  -- 主键,模號         
    DBJ Number(10,2), --得標價              
    KB CHAR(10), --課別                     
    Y Number (10), --年                     
    M Number (10), --月                      
    Zdbj Number (10,2), --總得標價       
    jd_bfb Number (10,2), --稼動百分比             
    jdfy Number (10,2)) --稼動費用                
    create table jd(
    zjd NUMBER(10,2), --總稼動
    KB CHAR(10), --課別                     @
    Y Number (10), --年                     @
    M Number (10) ) --月 
      

  11.   


    --cost0表中的增、删、改触发cost表的修改:Create Or Replace Trigger biud_cost0
      Before insert or update or delete On cost0
      for each row
    --定义变量
    declare
      v_yn     number;
      v_zjd    NUMBER(10, 2);
      v_Zdbj   Number(10, 2);
      v_jd_bfb Number(10, 2);
      v_jdfy   Number(10, 2);
      cursor c1 is
        Select count(*)
          from cost
         where y = :new.y
           and m = :new.m
           and kb = :new.kb;
      cursor c2 is
        Select max(nvl(zjd, 0))
          from jd
         where y = :new.y
           and m = :new.m
           and kb = :new.kb;
      cursor c3 is
        Select sum(dbj)
          from cost
         where y = :new.y
           and m = :new.m
           and kb = :new.kb;
      --计算并更新
    begin
      open c1;
      fetch c1
        into v_yn;
      open c2;
      fetch c2
        into v_zjd;
      open c3;
      fetch c3
        into v_zdbj;
      if inserting  and (v_yn < 1) then
        v_Zdbj   := :new.dbj;
        v_jd_bfb := 1;
        v_jdfy   := v_zjd;
        insert into cost
        values
          (:new.dbj, :new.kb, :new.y, :new.m, v_zdbj, 1, v_zjd);
      end if;
      if v_yn >= 1 then
        v_Zdbj   := v_zdbj + :new.dbj;
        v_jd_bfb := :new.dbj / v_zdbj;
        v_jdfy   := v_zjd * v_jd_bfb;
        update cost
           set zdbj = v_zdbj, jd_bfb = v_jd_bfb, jdfy = v_jdfy
         where kb = :new.kb
           and y = :new.y
           and m = :new.m;
      end if;
      --if deleting then
      --   delete from cost
      --   where y = :new.y
      --     and m = :new.m
      --     and kb = :new.kb;
      --end if;
      close c1;
      close c2;
      close c3;
    end biud_cost;
      

  12.   


    --查看一些结果:SQL> Insert into cost0(mh,dbj,kb,y,m) values('HS1101001','5000','鉗工一課','2008','11');1 row insertedSQL> commit;Commit completeSQL> select * from cost0;MH                                      DBJ KB                   Y           M
    ------------------------------ ------------ ---------- ----------- -----------
    HS1101001                           5000.00 鉗工一課          2008          11SQL> select * from cost;         DBJ KB                   Y           M         ZDBJ       JD_BFB         JDFY
    ------------ ---------- ----------- ----------- ------------ ------------ ------------
         5000.00 鉗工一課          2008          11      5000.00         1.00 SQL> select * from jd;         ZJD KB                   Y           M
    ------------ ---------- ----------- -----------SQL> Insert into cost0(mh,dbj,kb,y,m) values('HS1101002','8000','鉗工二課','2008','11');1 row insertedSQL> commit;Commit completeSQL> select * from cost0;MH                                      DBJ KB                   Y           M
    ------------------------------ ------------ ---------- ----------- -----------
    HS1101001                           5000.00 鉗工一課          2008          11
    HS1101002                           8000.00 鉗工二課          2008          11SQL> select * from cost;         DBJ KB                   Y           M         ZDBJ       JD_BFB         JDFY
    ------------ ---------- ----------- ----------- ------------ ------------ ------------
         5000.00 鉗工一課          2008          11      5000.00         1.00 
         8000.00 鉗工二課          2008          11      8000.00         1.00 SQL> select * from jd;         ZJD KB                   Y           M
    ------------ ---------- ----------- -----------SQL> Insert into cost0(mh,dbj,kb,y,m) values('HS1101003','1000','鉗工一課','2008','11');1 row insertedSQL> commit;Commit completeSQL> select * from cost0;MH                                      DBJ KB                   Y           M
    ------------------------------ ------------ ---------- ----------- -----------
    HS1101003                           1000.00 鉗工一課          2008          11
    HS1101001                           5000.00 鉗工一課          2008          11
    HS1101002                           8000.00 鉗工二課          2008          11SQL> select * from cost;         DBJ KB                   Y           M         ZDBJ       JD_BFB         JDFY
    ------------ ---------- ----------- ----------- ------------ ------------ ------------
         5000.00 鉗工一課          2008          11      6000.00         0.17 
         8000.00 鉗工二課          2008          11      8000.00         1.00