5

列の値を分割して、1つの列から複数の行を挿入したい。しかし、パフォーマンスの問題のため、カーソルなしでそれを行う必要があります。

すべてvalueが6文字の長さの値に分割されます。次に、これらの値も3、1、および2文字の長さの値に分割され、テーブルBに異なる列が挿入されます。

サンプルを提供することで私の質問が明確になると思います。

表A

ID      Value
1       ABCDEFGHJKLM
2       NOPRST
3       NULL VALUE

これらの値をこの形式のようにテーブルBに挿入したい

表B

ID     Value1       Value2       Value3
1       ABC          D            EF
1       GHJ          K            LM
2       NOP          R            ST
4

3 に答える 3

6

値の最大長として600(100行)を想定します。

insert into tableB
select id, substr(value,n*6+1,3), substr(value,n*6+4,1), substr(value,n*6+5,2)
from tableA 
     join (select level-1 as n from dual connect by level <= 100)
       on length(value) > n*6;

Sqlfiddleを参照してください。

于 2012-09-07T11:07:59.877 に答える
3
select ID,
    SUBSTR(value,number*6+1,3),
    SUBSTR(value,number*6+4,1),
    SUBSTR(value,number*6+5,2)
from yourtable,
    (select 0  as number union select 1 union select 2 union select 3 union select 4 
            union  select 5 union select 6) as numbers 
      /* etc up to the max length of your string /6 */
where LEN(value)>number*6   
于 2012-09-07T11:21:09.163 に答える
0

これを試して:

ORACLE SQLに変換してください。whileループを使用し、一括挿入を実行します。ループは、テーブルの値の最大長の長さに従って実行されます。

declare @max_len int=0;
declare @counter int=0;
declare @col_index int=1;
select @max_len=MAX(len(Value)) from TableA

while (@max_len/6 > @counter)
begin
    set @counter=@counter+1

    Insert into TableB

    select ID,substring(Value,@col_index,3),
              substring(Value,@col_index+3,1),
              substring(Value,@col_index+4,2) 
    from TableA where substring(Value,@col_index,3) is not null

    set @col_index=@col_index+6
end
于 2012-09-07T11:17:33.320 に答える