在PL / SQL中,我想在游标中声明一个游标。第二个游标应该使用“FROM子句”中第一个游标的值。我怎样才能做到这一点?

时间:2016-02-11 10:14:07

标签: oracle plsql

我需要在cursor条款中读取FROM到另一个/* First Cursor to getTable Name*/ Cursor Get_Table_Nm IS Select code_val1_nm from ref_table_detail where CODE_VAL2_NM=V_Table_Name /*Second Cursor to update the id*/ CURSOR SEQ_UPDT IS SELECT V_Table_Name_Intrl_Id, V_Table_Name_Seq_Id FROM (<<<<<Here I Want to pass the value from Get_Table_Nm (first) Cursor>>>>); 的值。 以下是代码段。

public interface Foo {
  static Foo get() {
    ...
  }
}

2 个答案:

答案 0 :(得分:0)

此处cursor_1返回table name需要在second cursor

中使用的dynamic cursor

您必须使用DECLARE Cursor Get_Table_Nm IS Select code_val1_nm from ref_table_detail where CODE_VAL2_NM = V_Table_Name; SEQ_UPDT SYS_REFCURSOR; v_code_val1_nm varchar2(50); BEGIN OPEN Get_Table_Nm; LOOP FETCH Get_Table_Nm INTO v_code_val1_nm; EXIT WHEN Get_Table_Nm%NOTFOUND; open SEQ_UPDT for 'SELECT V_Table_Name_Intrl_Id, V_Table_Name_Seq_Id FROM ' || v_code_val1_nm; LOOP -- Fetch SEQ_UPDT into .... -- Do with the data END LOOP; close SEQ_UPDT END LOOP; close Get_Table_Nm; END;

{{1}}

答案 1 :(得分:0)

如果需要在不同的表上打开游标,根据查询结果,可以尝试使用动态SQL;例如以下内容(伪代码):

DECLARE
    V_Table_Name                varchar2(30);
    V_Table_For_Second_Query    varchar2(30);
    ...
BEGIN
    ...       
    Select code_val1_nm 
    into V_Table_For_Second_Query
    from ref_table_detail 
    where CODE_VAL2_NM=V_Table_Name;

    open cursor SEQ_UPDT for 
        'SELECT V_Table_Name_Intrl_Id, V_Table_Name_Seq_Id ' ||
        'FROM ' ||
        V_Table_For_Second_Query;
    ...