2017-05-10 26 views
0

在下面的查詢中,我不能在最內層的查詢中使用c.college_id,因爲它超出了作用域,但是有什麼解決方案可以在最內層查詢中使用c.college_id值我不想在裏面調用'coll_details',因爲它會影響性能。變量作用域在查詢中的內部遊標中不可用

WITH coll_details AS (SELECT cd.college_id 
         , cd.college_name 
        FROM  college_details cd 
       ) 
SELECT c.college_name 
    CURSOR (SELECT o.department 
       FROM  (SELECT tc.department 
           , ROW_NUMBER() OVER(ORDER BY tc.college_id) rnum 
         FROM temp_college tc 
         WHERE tc.college_id = c.college_id 
         ) o 
      WHERE  o.rnum BETWEEN 1 AND 3 
      ) 
FROM  coll_details c 

回答

0

你需要做的是包括在最裏面的子查詢college_id然後過濾光標聲明:

SELECT c.college_name 
    , CURSOR (select department from (
        SELECT tc.college_id 
          , tc.department 
          , row_number() over (partition by tc.college_id 
                order by tc.department_id) rn 
       FROM temp_college tc 
       ) tc 
        where tc.college_id = c.college_id 
        and rn <= 3 
      ) coll_depts 
FROM college_details c 
/

演示(內嵌光標輸出醜陋)。

COLLEGE_NAME  COLL_DEPTS 
---------------- -------------------- 
FOX COLLEGE  CURSOR STATEMENT : 2 

CURSOR STATEMENT : 2 

DEPARTMENT 
-------------------- 
PHYSICS 
LATIN 

BEETLE COLLEGE CURSOR STATEMENT : 2 

CURSOR STATEMENT : 2 

DEPARTMENT 
-------------------- 
ETYMOLOGY 
EARTH SCIENCE 

KNOX INSTITUTE CURSOR STATEMENT : 2 

CURSOR STATEMENT : 2 

DEPARTMENT 
-------------------- 
ASTROPHYSICS 
COMPUTER SCIENCE 


SQL> 

注意:由於示例數據,演示使用2的分頁。

+0

我編輯了這個問題請看看..我在這裏使用分頁概念,所以我必須使用內部光標 – manikandan

+0

爲了將來的參考,請張貼一整套要求。發佈不完整或半成品的問題只會浪費人們的時間。這是沒有辦法對待你要求幫助你的人。 – APC

相關問題