公式样例:
A059.G05919 := G007.G00703 + G008.G00704 +100.43
像A059 和G007 G008这是数据库的表名。
而表名后面是字段名。
现在需要写一个从出过程来实现这个公式。把右面执行的结果更新到左边的字段中。
因为初学存储过程,觉得很难。不知道大哥哥大姐姐们谁能帮帮忙。

解决方案 »

  1.   


    你这三个表A059 和G007 G008按什么字段去关联?
    把简单的数据帖出来,应该用一条SQL就可以解决了.
      

  2.   

    declare
      i   int;
     begin
      execute immediate 'select 1+1 from dual'
        into i;
      dbms_output.put_line(i);
     end;可以输出2
      

  3.   

    这三个表关联的条件是 A059.a05900 = G007.g00700 =G008.g00800
      

  4.   

    我和二楼的想法一样,
    如果不太难的话,不一定要用存储过程
    用SQL直接解决!
      

  5.   

    这个直接写一个update语句就可以了呀
      

  6.   

    update A059 set G05919=(select 
    G007.G00703 + G008.G00704 +100.43 
    from G007,G008 where G007.g00700 =G008.g00800 and A059.a05900 = G007.g00700)
      

  7.   

    tryupdate A059
       set G05919 = (select G00703 from G007 where A059.a05900 = G007.g00700) +
                    (select G00704 from G008 where A059.a05900 = G008.g00800) +
                    100.43
     where exists (select G00703 from G007 where A059.a05900 = G007.g00700)
       and exists
     (select G00704 from G008 where A059.a05900 = G008.g00800)
      

  8.   

    update A059
       set G05919 = (select G00703 + G00704 + 100.43
                       from G007
                      where A059.a05900 = G007.g00700
                        and A059.a05900 = G008.g00800) where exists (select 1
              from G007
             where A059.a05900 = G007.g00700
               and A059.a05900 = G008.g00800)
      

  9.   

    不好意思语法错误:update A059
       set G05919 = (select G00703 + G00704 + 100.43
                       from G007
                      where A059.a05900 = G007.g00700
                        and A059.a05900 = G008.g00800) where exists (select 1
              from G007
             where A059.a05900 = G007.g00700
               and A059.a05900 = G008.g00800)
      

  10.   

    写存储过程。思路很简单的。把字段作为参数,写个动态的sql语句就OK了
      

  11.   

    说的对,多少张表是不确定的,表的关联就是用表名+00的在字段进行关联的。但是传入的只有表达式。
    需要在oracle中把表达式进行分解。
      

  12.   


    create or replace procedure test100(gongshi in varchar2)
    as
    strsql varchar2(4000);begin
         strsql:='update A059
       set G05919 = (select '||gongshi||'  from G007,G008
                      where A059.a05900 = G007.g00700
                        and A059.a05900 = G008.g00800)
                       where exists (select 1
              from G007,G008
             where A059.a05900 = G007.g00700
               and A059.a05900 = G008.g00800)';
        execute immediate strsql;
        commit;           
    end;
    这个过程只针对A059的G05919 字段和G007及G0082个表。
    调用call test100('G007.G00703 + G008.G00704 +100.43' )
      

  13.   

    刚刚lz的帖子结的很快,参考
    http://topic.csdn.net/u/20081217/14/3d894130-a4ed-4bb4-aef3-dcac7db4a4bd.htmlselect REGEXP_SUBSTR(f,'[A-Z][0-9]+')as f1,REGEXP_SUBSTR(f,'[^.][A-Z][0-9]+') as f2,f from(
    select 'A059.G05919 := G007.G00703 ' as f from dual) /*
     F1    F2    F
    A059     G007    A059.G05919 := G007.G00703 
    */
      

  14.   

    IF A058.E05805 / 7 < 2 AND A058.G05812 = 0 
    A059.G05937 := A059.G05937 +(A059.G05902 + A059.A05903+A059.G05936) * 0.4 / 21.75 * A059.G05925 ENDIF 
    假如遇到这样的公式呢!
      

  15.   


    你不觉得这样设计的系统很难使用么。是不是让用户去写if else?
      

  16.   

    这个是有点麻烦~那传入公式的时候就不要用IF,用CASE好了。create or replace procedure test100(gongshi in varchar2)
    as
    strsql varchar2(4000);begin
         strsql:='update A059
       set G05919 = (select '||gongshi||'  from G007,G008,A059
                      where A059.a05900 = G007.g00700
                        and A059.a05900 = G008.g00800)
                       where exists (select 1
              from G007,G008,A059
             where A059.a05900 = G007.g00700
               and A059.a05900 = G008.g00800)';
        execute immediate strsql;
        commit;           
    end;
    调用的方式,即传入的参数变一下call test100(case when A058.E05805 / 7 < 2 AND A058.G05812 = 0 
    then A059.G05937 +(A059.G05902 + A059.A05903+A059.G05936) * 0.4 / 21.75 * A059.G05925 else A059.G05919 END ')
      

  17.   

    那就要增加变量了~等号后面的参数个数不是问题,只要都是G007和G008中的字段就可以。如果你要更新的表名不是固定的,更新的列也不是固定的,那你就还要提供关联字段create or replace procedure test100(gongshi in varchar2,tablename in varchar2,fieldname in varchar2)
    as
    strsql varchar2(4000);begin
         strsql:='update '||tablename||
       ' set '||fieldname||' = (select '||gongshi||'  from G007,G008,A059
                      where A059.a05900 = G007.g00700
                        and A059.a05900 = G008.g00800)
                       where exists (select 1
              from G007,G008,A059
             where A059.a05900 = G007.g00700
               and A059.a05900 = G008.g00800)';
        execute immediate strsql;
        commit;           
    end;
      

  18.   

    --写一个函数
    CREATE OR REPLACE FUNCTION F_test(V_a           IN NUMBER,
                                          V_b       IN NUMBER
                                          ) RETURN VARCHAR2 IS
      V_c NUMBER(9, 5);BEGIN
    --这里面写上你的公式,如果需要逻辑判断的话,就加上if  else或者case when 等判断结构
      V_c:=V_a+V_b+xxx;
      RETURN(V_c);
    END F_test;--调用该函数
    insert into table_a
    select F_test(table_b.col1,table_c.col2)
    from table_b ,table_c
    where table_b.colx=table_c.colx;
    commit;
      

  19.   

    创建一个函数(function),试一下使用
    return A059.G05937 := A059.G05937 +(A059.G05902 + A059.A05903+A059.G05936) * 0.4 / 21.75 * A059.G05925
    来返回值