2015-11-18 86 views
0

我有2個表格,外加第三個表格,將數據輸入到表格中。PL/SQL遊標幫助

表是:

SQL> select * 
2 from paydata1; 

IDNO NAME      J  SALARY  PAYHR 
---- ------------------------- - ---------- ---------- 
1111 Ann French    S  75000   0 
2222 Robert Costa    H   0   45 
3333 Linda Ames    H   0   50 
4444 Scott Brooks    S  78000   0 
5555 Susan Ash     S  57000   0 
6666 James Smith    S  55000   0 
7777 Mary Jones    H   0   36 
8888 John Morse    H   0   39 

SQL> select * 
2 from paytran1; 

IDNO PRO HOURSWK 
---- --- ---------- 
1111 123   20 
1111 456   10 
1111 789   15 
2222 123   17 
2222 456   24 
2222 789   20 
3333 123   20 
3333 789   20 
4444 123   10 
4444 456   28 
4444 789   5 
5555 456   40 
6666 456   44 
7777 456   30 
7777 789   15 
8888 123   10 
8888 456   25 
8888 789   5 

的問題是:寫的PL/SQL程序中使用遊標來讀取文件和寫出來的工作總時間每個員工。

我的PL/SQL代碼爲:

SET SERVEROUTPUT ON 
DECLARE 
    v_idno  paydata1.idno%TYPE; 
    v_name  paydata1.name%TYPE; 
    v_idno1  paytran1.idno%TYPE; 
    v_hourswk paytran1.hourswk%TYPE; 
    CURSOR paydata1_cursor IS 
    SELECT idno, name FROM paydata1 
    ORDER BY idno; 
    CURSOR paytran1_cursor IS 
    SELECT idno, hourswk FROM paytran1 
    WHERE v_idno1 = idno 
    order BY idno; 
    BEGIN 
    OPEN paydata1_cursor; 
    LOOP 
     FETCH paydata1_cursor INTO v_idno, v_name; 
     EXIT WHEN paydata1_cursor%NOTFOUND; 
     IF paytran1_cursor%ISOPEN THEN 
     CLOSE paytran1_cursor; 
     END IF; 
     OPEN paytran1_cursor; 
     LOOP 
     FETCH paytran1_cursor INTO v_idno1, v_hourswk; 
     EXIT WHEN paytran1_cursor%NOTFOUND; 
     END LOOP; 
     INSERT into allinfo 
      VALUES(v_idno, v_name, v_hourswk); 
     CLOSE paytran1_cursor; 
    END LOOP; 
    CLOSE paydata1_cursor; 
END; 
/
SET SERVEROUTPUT OFF 

我allinfo表的結果是:

SQL> select * 
    2 from allinfo; 

IDNO EMPLOYEE    HOURS 
---- --------------- ---------- 
1111 Ann French 
2222 Robert Costa 
3333 Linda Ames 
4444 Scott Brooks 
5555 Susan Ash 
6666 James Smith 
7777 Mary Jones 
8888 John Morse 

爲什麼心不是在hourswk正在與IDNO組合並顯示在allinfo表和名字?

回答

2

嘗試使paytran1_cursor一個參數光標:

CURSOR paytran1_cursor (p_idno1 paydata1.idno%TYPE) IS 
    SELECT SUM(hourswk) FROM paytran1 -- already have idno; just sum hours 
    WHERE p_idno1 = idno -- don't need to order by one value 
[...] 

-- sum() will return one row, so no need to loop 
OPEN paytran1_cursor (v_idno1); 
FETCH paytran1_cursor INTO v_hourswk; 

(我認爲這是甲骨文的語法。)

這裏有幾個問題。主要的一點是當你的光標查詢運行時,被寫爲,當光標爲時打開。這意味着當您打開paytran1_cursor時,您正在使用當時的值v_idno1查詢,該值爲NULL。這裏使用參數光標將

  1. 確保您傳遞到您的WHERE子句中的值的範圍是, 和
  2. 說清楚讀者,你是空調來對數據光標 查詢從光標外部。

希望有所幫助。

+0

我使用了你的修復程序,它仍然不會計數hourswk,就像我以前的輸出 –

+0

做了一些改變。它現在工作嗎?我很抱歉我的甲骨文有點生疏。 –

+0

我不得不重新插入訂單,以使其起作用,但仍未插入到理貨或小時工 –

0

通過尋找代碼。我認爲更好的方法是使純粹的SQL方法,而不是使用PLSQL方法。我已經以這樣的方式複製了場景,以便您可以實施它。 Mey be這會幫助你。

INSERT INTO <TABLE> 
SELECT A.IDNO, 
    SUM(A.PSUM+B.HSWR) 
FROM 
    (SELECT PAYHR.IDNO, 
    SUM(PAYHR.PAYHR) PSUM 
    FROM 
    (SELECT 111 AS IDNO,23 AS PAYHR FROM DUAL 
    UNION ALL 
    SELECT 111 AS IDNO,30 AS PAYHR FROM DUAL 
    UNION ALL 
    SELECT 112 AS IDNO,10 AS PAYHR FROM DUAL 
    UNION ALL 
    SELECT 112 AS IDNO,8 AS PAYHR FROM DUAL 
    UNION ALL 
    SELECT 112 AS IDNO,15 AS PAYHR FROM DUAL 
    UNION ALL 
    SELECT 113 AS IDNO,25 AS PAYHR FROM DUAL 
    )PAYHR 
    GROUP BY PAYHR.IDNO 
)A, 
    (SELECT HSWRK.IDNO, 
    SUM(HSWRK.HSWR) HSWR 
    FROM 
    (SELECT 111 AS IDNO,10 HSWR FROM DUAL 
    UNION ALL 
    SELECT 111 AS IDNO,30 HSWR FROM DUAL 
    UNION ALL 
    SELECT 111 AS IDNO,15 HSWR FROM DUAL 
    UNION ALL 
    SELECT 111 AS IDNO,25 HSWR FROM DUAL 
    UNION ALL 
    SELECT 112 AS IDNO,10 HSWR FROM DUAL 
    UNION ALL 
    SELECT 112 AS IDNO,17 HSWR FROM DUAL 
    UNION ALL 
    SELECT 112 AS IDNO,40 HSWR FROM DUAL 
    UNION ALL 
    SELECT 113 AS IDNO,16 HSWR FROM DUAL 
    )HSWRK 
    GROUP BY HSWRK.IDNO 
)B 
WHERE A.IDNO = B.IDNO 
GROUP BY A.IDNO; 
+0

我希望我可以做純SQL,它必須在PL/SQL中完成遊標,它讓我頭痛 –

+0

嘿,如果最後的任務是插入表中的總小時數,那麼這也可以去吧:) –