表中具有列名的插入值在插入时应增加

时间:2019-11-27 11:13:34

标签: sql oracle oracle11g

我有表格activity_log。 在活动日志表中,只有一列,其中包含50条记录。

我创建了一个虚拟表/测试表,其中包含ID作为第一列,并在活动日志表中动态创建了50列bcoz,其中有50行,但是现在在50列中插入50行值时。 但是我不知道如何在虚拟表的50列中插入/更新50行的值?

declare

        i number;
        sql_stmnt  varchar2(400);
        xx   varchar2(400);
        b  varchar2(400);
        res number;
        end_char number;
        j number;
begin

        i:=0;
        xx:='a';
        end_char:=1;
        j:=1;

    select count(var_activity_pagetitle) into end_char  from activity_log;
    for i in 1..end_char loop 
            if i>=26 and i<=48 then
            select concat(xx,'a') into xx from dual;
            end if;
            if i>=49 and i<=74 then
            select concat(xx,'aa') into xx from dual;
            end if;
            if i>=75 and i<=100 then
            select concat(xx,'hh') into xx from dual;
            end if;

    sql_stmnt :=  'ALTER TABLE test_cursor_add ADD ' ||  xx ||  ' varchar2(1000)'; 

    dbms_output.put_line(sql_stmnt );
    Execute immediate  sql_stmnt ;
    dbms_output.put_line(i);

    select   ASCII( 'a' )+j into res  from dual;
        if j>=25 then
        j:=1;
        end if;

  select CHR( res ) into b from dual;
  xx:=b;

        if(i=50 ) then
        return;
        end if;

  j:=j+1;

  end loop;

end;

----

create table test_cursor_add ( id number);

1 个答案:

答案 0 :(得分:1)

您可以按以下方式使用PIVOT。它将生成包含51列(包括id和50列)的结果。然后,您可以将其插入test_cursor_add表中。

Insert into test_cursor_add
(Id,
Col1,
Col2,
...
Col50)
(Select * from
(Select 1 as id, t.var_activity_pagetitle,
        Row_number() over (order by null) as rn
        From activity_log t)
Pivot
(Max(var_activity_pagetitle) for rn in (1,2,3,...,50))

干杯!

相关问题