带有结果的Oracle db和pl / sql游标组

时间:2013-11-11 22:03:41

标签: sql oracle plsql cursor procedure

大家好! 我对我的oracle db过程有疑问: (对于我的英语和不清楚的演讲,我道歉) 我写了简单的程序(但即使不编译它,所以我不知道工作是不是),但我不喜欢它,cos selec in循环。 有时候我用group by和result写了一些sql选择。所以它是可能的,我怎么能(在pl / sql中)使用结果。 附:对不起,如果它是愚蠢的,但我不知道,我必须写在谷歌。

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;
/

1 个答案:

答案 0 :(得分:1)

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