2014-11-16 50 views
0

我想根據同一腳本中使用的幾個pl-sql變量動態生成遊標查詢。考慮一個例子動態生成遊標查詢

1. DECLARE 
2.  emp_id number(12); 
3.  CURSOR empList_cur is select emp_id from employee; 
4.  
5. BEGIN 
6.  LOOP 
7.   FETCH empList_cur Into emp_id ; 
8.   EXIT WHEN customerList_cur%NOTFOUND; 
9.   CURSOR activityList_cur is (select empDetails from empDet where empNo=:emp_id) 
10.  END LOOP; 
11. END; 

我想使用從一個光標到其他SQL查詢檢索EMP_ID的值如在上面的代碼段第9行。

如果語法正確,該怎麼辦?

在此先感謝

回答

0

您查詢只接受一個值動態的,而不是動態SQL本身。

DECLARE 
    l_emp_id number(12); /* Don't use the column name itself as variable name */ 
    CURSOR empList_cur is select emp_id from employee; 

BEGIN 
    LOOP 
     FETCH empList_cur Into l_emp_id ; 
     EXIT WHEN customerList_cur%NOTFOUND; 
     FOR I IN (select empDetails from empDet where empNo=l_emp_id) 
     LOOP 
      /* you can simply use the variable here , 
      and using a Cursor here is not efficient */ 
     -- Other statements 
     END LOOP; 
    END LOOP; 
END;