表A,
字段A,B  
字段A内容为','搁开的字符串(每个搁开的值对应表AA的A1字段)
A                         B
13450,1234,145,643,       X表AA
字段A1,A2   主键为A1
A1           A2
13450        T
1234         TT
145          TTT
643          TTTT能不能有个sql,执行好更新表A得到:A                         B
T,TT,TTT,TTTT             X

解决方案 »

  1.   

    declare
    v_temp varchar(30);
    cursor a1_cur is
      select A2 from AA;
    begin
      for a1_info in a1_cur
      loop
        v_temp := v_temp||','||a1_info.A2;
      end loop;
      update A
      set A=v_temp
      where B='X';
    end;
      

  2.   

    怎么不行 报什么错误
    我这里没有Oracle
      

  3.   

    没报错,但结果还是
    A                         B
    13450,1234,145,643,       X
      

  4.   

    不会吧游标a1_cur取的是AA表的A2的值
    然后把游标的值取出来,通过v_temp := v_temp||','||a1_info.A2;
    V_temp的值就应该是T,TT,TTT,TTTT
      

  5.   

    create or replace proceudre pr_unit
    declare
         ls_tem  varchar(50);
         ls_new  varchar(15);
         ls_pat  varchar(15);
         li_pos  number(3);
         li_pre  number(3);
    begin
        for cr_cur in (select A || ',' as A, B from A) loop
            ls_tem := '';
            li_pre := 1;
            li_pos :=instr(cr_cur.A, ',', li_pre);
            while li_pos >0 loop
                ls_pat := substr(cr_cur.A, li_pre, li_pos - li_pre);
                select A2 into ls_new from AA where A1 = ls_pat;
                ls_tem := ls_tem || ',' || ls_new;
                
                li_pre := li_pos + 1;
                li_pos := instr(cr_cur.A, ',', li_pre);
            end loop;
       
            update A set A = ls_tem where A = cr_cur.A;
        end loop;
    end;
      

  6.   

    寫少了一句,在UPDATE 前再寫一句ls_tem := trim(',' from ls_tem);
    update A set A = ls_tem where A = cr_cur.A;
      

  7.   

    chenro(大地雄心) ,编译不过啊
      

  8.   

    tongyu10068() ,还是不行,你找个环境测试一下
      

  9.   

    tongyu10068() ,你没有明白我的意思:
    V_temp的值就应该是T,TT,TTT,TTTT,这个是没有错,但是如果,表A,字段A内容为','搁开的字符串,这些搁开的子串not in 表AA的A1字段,不应该更新到表A的字段A中,
    比如:
    表A,
    字段A,B  
    字段A内容为','搁开的字符串(每个搁开的值对应表AA的A1字段)
    A                            B
    13450,1234,145       X表AA
    字段A1,A2   主键为A1
    A1           A2
    13450        T
    1234         TT
    145          TTT
    643          TTTT
    输出应该为T,TT,TTT,不应该为T,TT,TTT,TTTT,因为TTTT对应的643 not in 表A 的字段A(搁开后)
      

  10.   

    chenro(大地雄心) ,你在你机器上试试,就知道了啊
      

  11.   

    declare
    v_temp varchar(30);
    cursor a1_cur is
      select A2 from AA;
    begin
      for a1_info in a1_cur
      loop
        v_temp := v_temp||','||a1_info.A2;
      end loop;
      update A
      set A=v_temp
      where B='X';
    end;
    这样是可以的,我测试了,只不过多了一个“,”,其他的都是正确的!
      

  12.   

    呵呵,只是打錯一點點啊create or replace procedure pr_unit
    as
         ls_tem  varchar(50);
         ls_new  varchar(15);
         ls_pat  varchar(15);
         li_pos  number(3);
         li_pre  number(3);
    begin
        for cr_cur in (select A || ',' as A, B from A) loop
            ls_tem := '';
            li_pre := 1;
            li_pos :=instr(cr_cur.A, ',', li_pre);
            while li_pos >0 loop
                ls_pat := substr(cr_cur.A, li_pre, li_pos - li_pre);
                select A2 into ls_new from AA where A1 = ls_pat;
                ls_tem := ls_tem || ',' || ls_new;
                
                li_pre := li_pos + 1;
                li_pos := instr(cr_cur.A, ',', li_pre);
            end loop;
            ls_tem := trim(',' from ls_tem);
            update A set A = ls_tem where A = trim(',' from cr_cur.A);
        end loop;
    end;
      

  13.   

    update a set 
      a = (
        select f_conc( aa.a2 ) from aa, table(f_sep( a.a )) c
        where c.c=aa.a1
      )
      

  14.   

    那肯定不給分了﹐別人說用一個SQL呢﹐看你寫的
      

  15.   

    哈哈哈.....我以前好想見過用一條SQL的不過一時記不起來....
      

  16.   

    這樣的問題不用過程我真的不會,有的話能告訴我嗎?我再開貼,給200分
    我的要求是這樣有一個表
    tableA  
    col1, col2
    30a   30
    32aa  50
    40d   25要求用一個SQl 生成 
    30a/32aa/40d   105
      

  17.   

    哈哈哈﹐﹐﹐結果出來了二百分要給我哦!........select (co||'/'||ct||'/'||ch)  col1,scol2 col2 from
    (
    select scol2,max(decode(col1,'30a',col1,null)) co,
                 max(decode(col1,'32aa',col1,null)) ct,
                 max(decode(col1,'40d',col1,null)) ch
    from (select col1,sum(col2) over() as scol2 from cta) group by scol2
    )
    里面的cta是tableA
      

  18.   

    vpdlsr
    問題是 記錄不是固定的
      

  19.   

    那可以要用動態SQL﹐或者在寫程序員把它串起來
      

  20.   

    這個可以了﹐我測試過﹐不過如果表的記錄多了就要用程序來控制他把SQL語句串起來..update csdn_a set a = 
    (
    select col1||','||col2||','||col3 tcol
    from
    (
    select chgb,max(decode(chga,'T',chga)) col1, max(decode(chga,'TT',chga)) col2, max(decode(chga,'TTT',chga)) col3
    from
    (
    select chgb,chga 
    from
    (
    select totala.b chgb, csdn_aa.a2 chga
    from
    (
    select b,substr(a,1,instr(a,',',1,1)-1) as name from csdn_a 
    union all
    select b,substr(a,length(substr(a,1,instr(a,',',1,1)))+1,(length(substr(a,1,instr(a,',',1,2)))-1) - (length(substr(a,1,instr(a,',',1,1))))) as name 
    from csdn_a 
    union all
    select b,substr(a,length(substr(a,1,instr(a,',',1,2)))+1,(length(substr(a,1,instr(a,',',1,3)))-1) - (length(substr(a,1,instr(a,',',1,2))))) as name 
    from csdn_a 
    union all
    select b,substr(a,length(substr(a,1,instr(a,',',1,3)))+1,(length(substr(a,1,instr(a,',',1,4)))-1) - (length(substr(a,1,instr(a,',',1,3))))) as name 
    from csdn_a
    )totala ,csdn_aa
    where totala.name = csdn_aa.A1
    order by chga
    )
    )
    group by chgb
    )
    )
    where b = 'X'