Search code examples
sqloracleplsqlcursorprocedure

Oracle db and pl/sql cursor group with result


Good day, everyone! I have question with my oracle db procedure: (I apologize, for my English and unclear speech) I wrote simple procedure (but even don't compile it, so i don't know work is it or no), but i don't like it, cos selec in loop. Sometime age i wrote some sql selections with group by and result. So it's possible and how can i (in pl/sql) working with result. P.s. Sorry, if it was and stupid, but i don't know, what i must write in google.

CREATE OR REPLACE PROCEDURE getDescription
(
        columnName IN VARCHAR2
)
AS
        CURSOR descriptionSelector IS
        SELECT utc.table_name tableName, utc.data_type columnType, utc.DATA_LENGTH columnLenth
        FROM user_tab_columns utc
        WHERE utc.column_name = columnName;

        fetched descriptionSelector%ROWTYPE;
        index INTEGER;
BEGIN
        index := 1;
        DBMS_OUTPUT.PUT_LINE(rpad('No.',4)||rpad('Column',25)||rpad('Table',25)||rpad('Attribute',50));
        FOR currentSelect IN descriptionSelector
        LOOP
                DBMS_OUTPUT.PUT_LINE(rpad(index ,4) || ' ' ||rpad(columnName, 25)||' ' ||rpad(currentSelect.tableName,25)||' '||rpad('TYPE: '||currentSelect.columnType,20)' '||lpad('('||currentSelect.columnLenth||')',3));
                FOR secondSelection IN (
                        SELECT ref_ucc.table_name tableName, ref_ucc.column_name referenceColumnName, ref_ucc.constrain t_name
                        FROM user_cons_columns ucc
                        JOIN user_constraints uc
                                ON ucc.constraint_name = uc.constraint_name
                        JOIN user_cons_columns ref_ucc
                                ON uc.r_constraint_name = ref_ucc.constraint_name
                        WHERE uc.constraint_type = 'R'
                        AND ucc.table_name = currentSelect.tableName
                        AND ucc.column_name = columnName)
                LOOP
                        DBMS_OUTPUT.PUT_LINE('Constr: '||secondSelection.constraint_name||' References '||secondSelection.tableName||'('||secondSelection.referenceColumnName||')');
                END LOOP;
                index := index + 1;
        END LOOP;
END;
/

Solution

  • SELECT ref_ucc.table_name tableName, ref_ucc.column_name referenceColumnName, ref_ucc.constrain t_name
    FROM user_cons_columns ucc
    JOIN user_constraints uc
            ON ucc.constraint_name = uc.constraint_name
    JOIN user_cons_columns ref_ucc
            ON uc.r_constraint_name = ref_ucc.constraint_name
    WHERE uc.constraint_type = 'R'
    AND EXISTS
        (
            SELECT  1
            FROM  user_tab_columns utc
            WHERE utc.column_name = columnName
            AND   ucc.table_name = utc.tableName
        )
    AND ucc.column_name = columnName