Affichage des articles dont le libellé est PL SQL. Afficher tous les articles
Affichage des articles dont le libellé est PL SQL. Afficher tous les articles

lundi 12 décembre 2011

PL SQL : Cursor and imbricated table


The aim of this code is to get some values, including the name of table from all_tables of an Oracle Database,  and using this table_name in an instruction select ... from table_name.
First, I thought at 2 imbricated cursors but it is impossible to declare the second cursor :

cursor cur1 is select table_name, column_name from all_tables where conditions;
v_cur1 cur1%rowtype;
cursor cur2 is select * from v_cur1.table_name;
return a compilation error :  v_cur1.table_name : non-existing table or view.

There is one possible solution  :

declare

    cursor cur_tables is
    select table_name
    from all_tables;

    vr_cur_tables       cur_tables%rowtype;
    req_index           varchar2(400);
    nb_key_index        number;

    --create a record type object
    type keyIndex is record (index_name varchar2(20), index_value varchar2(20));
    --create a table type object composed of an index and a column of keyIndex, record type object
    type tKeyIndex is table of keyindex;
    tableKeyIndex tKeyIndex;
    i               number;
    req_nb_key varchar2(400);
     
begin
    for vr_cur_tables in cur_tables loop
        dbms_output.put_line(vr_cur_tables.table_name);
        -- select the data of the current table
        req_index := 'select * from ' || vr_cur_tables.table_name;
        -- set the data into a table
        execute immediate req_index bulk collect into tableKeyIndex;
        req_nb_key :='select count(*) from ' || vr_cur_tables.table_name;
        execute immediate req_nb_key into nb_key_index;
        --For each tupple of the select, display the column nomindex value of the current table
        for i in 1..nb_key_index loop
            dbms_output.put_line(tableKeyIndex(i).index_name);
        end loop;
    end loop;
end;

samedi 3 décembre 2011

PL SQL Oracle : Curseur avec tableau imbriqué

L'enjeu de ce code est de récupérer des valeurs de la table all_tables dont table_name puis d'utiliser ce table_name dans une instruction select ... from table_name. Spontanément j'ai pensé à l'utilisation de 2 curseurs imbriqués, tentative vite avortée du fait du problème de déclaration du second curseur.

cursor cur1 is select table_name, nom_colonnes from all_tables where conditions;
v_cur1 cur1%rowtype;
cursor cur2 is select * from v_cur1.table_name;
génère une erreur de compilation de type  v_cur1.table_name : table ou vue inexistante.

une solution possible est le code suivant :

declare

    cursor cur_tables is
    select nomtable
    from toutestables;

    vr_cur_tables       cur_tables%rowtype;
    req_index           varchar2(400);
    nb_key_index        number;

    --crée un objet de type record
    type keyIndex is record (nomindex varchar2(20), valindex varchar2(20));
    --crée un objet de type tableau composé d'un index et d'une colonne contenant un objet keyIndex de type  record
    type tKeyIndex is table of keyindex;
    tableKeyIndex tKeyIndex;
    i               number;
    req_nb_key varchar2(400);
     
begin
    for vr_cur_tables in cur_tables loop
        dbms_output.put_line(vr_cur_tables.nomtable);
        -- sélectionne les données de la table en cours
        req_index := 'select * from ' || vr_cur_tables.nomtable;
        -- récupère les données du select dans une table
        execute immediate req_index bulk collect into tableKeyIndex;
        req_nb_key :='select count(*) from ' || vr_cur_tables.nomtable;
        execute immediate req_nb_key into nb_key_index;
        --Pour chaque tupple du select affiche la valeur de la colonne nomindex de la table en cours
        for i in 1..nb_key_index loop
            dbms_output.put_line(tableKeyIndex(i).nomindex);
        end loop;
    end loop;
end;