Oracle
 sql >> Database >  >> RDS >> Oracle

Crea dinamicamente l'istruzione select in Oracle 12c

Puoi usarlo come base

declare
    upper_level number;
    t_sql varchar2(1000);
    l_sql varchar2(1000);
begin
    select lvl_cnt into upper_level from 
    SETUPS S,META S
    where s.id=m.id

    l_sql:='SELECT ID,
     Title,
     Desc,
     Type,'||
     upper_level


    for lvl in 1..upper_level
     loop
         t_sql:=t_sql||'type_'||lvl||','
     end loop;
     l_sql:=l_sql||t_sql
     l_sql:=rtrim(l_sql,',');
     l_sql:=l_sql||' FROM SCHEMA.TABLE
    WHERE ID = 1;';

end