0

如果我想从表中获取记录(表名是动态的输入),如何定义记录持有者或如何从这个定义的表中获取数据? p_table_name%rowtype将不符合要求,因为 p_table_name 是一个参数,而不是表名。

PROCEDURE do_scan(p_table_name IN VARCHAR2
    ,p_min_num IN NUMBER
    ,p_time_range IN NUMBER
    ,p_problem_desc OUT
    ,p_result_code OUT) 
IS
    TYPE ObjCurTyp  IS REF CURSOR;
    v_obj_cursor    ObjCurTyp;
    v_obj_record     ???????(p_table_name%rowtype)
BEGIN
    v_stmt_str := 'Select * from :t where date_started > TRUNC(SYSDATE-3)';
    OPEN v_obj_cursor FOR v_stmt_str USING p_table_name;

    LOOP
        FETCH v_obj_cursor INTO v_obj_record;
            EXIT WHEN v_obj_cursor %NOTFOUND;

    END LOOP;

END do_scan;
4

1 回答 1

1

您可以将该代码作为动态 PL/SQL 块放在 EXECUTE IMMEDIATE 语句中。

PROCEDURE do_scan
(
    p_table_name    IN VARCHAR2
    p_min_num       IN NUMBER
    p_time_range    IN NUMBER
    p_problem_desc  OUT
    p_result_code   OUT
) 
IS
BEGIN
    EXECUTE IMMEDIATE
        'DECLARE '                                           ||
        '    TYPE ObjCurTyp  IS REF CURSOR; '                ||
        '    v_obj_cursor    ObjCurTyp; '                    ||
        '    v_obj_record   ' || p_table_name || '%rowtype; '||
        'BEGIN '                                             ||
        '    v_stmt_str := ''Select * from :t where '        || 
        '        date_started > TRUNC(SYSDATE-3)''; '        ||
        '    OPEN v_obj_cursor '                             ||
        '        FOR v_stmt_str USING '                      || 
                 p_table_name || '; '                        ||
        '    LOOP '                                          ||
        '        FETCH v_obj_cursor INTO v_obj_record; '     ||
        '           EXIT WHEN v_obj_cursor %NOTFOUND; '      ||
        '    END LOOP; '                                     ||
        'END;';
END do_scan;

问候,

达里约什

于 2012-11-14T21:15:44.577 回答