PL / SQL,头部划痕

时间:2015-11-19 03:40:24

标签: oracle plsql

这是我的代码问题是它没有将hourswk添加到一起来为非工资员工正确地输出那些带有H的人.allinfo1中的hours列应该是45,61, 40,43,40,44,45,40,但它只显示一个小时而不添加它们。

    SET SERVEROUTPUT ON
DECLARE
  v_idno      paydata4.idno%TYPE;
  v_name      paydata4.name%TYPE;
  v_sal       paydata4.salary%TYPE;
  v_job       paydata4.jobcode%TYPE;
  v_pay       paydata4.payhr%TYPE;
  v_idno1     paytran1.idno%TYPE;
  v_hourswk   paytran1.hourswk%TYPE; 
  v_hours     allinfo1.hours%TYPE; 
  CURSOR paydata4_cursor IS
     SELECT idno, name, salary, jobcode, payhr FROM paydata4
     ORDER BY idno;
CURSOR paytran1_cursor IS
     SELECT idno, hourswk FROM paytran1
     WHERE v_idno = idno
     order by idno;
    BEGIN
     OPEN paydata4_cursor;
     LOOP
     FETCH paydata4_cursor INTO v_idno, v_name, v_sal, v_job, v_pay;
     EXIT WHEN paydata4_cursor%NOTFOUND;
       IF paytran1_cursor%ISOPEN THEN
        CLOSE paytran1_cursor;
       END IF;
       IF v_job = 'S'THEN
        v_sal := v_sal / 52;
       End if; 
       IF v_job = 'H'THEN
        v_sal := v_hours * v_pay;
       END IF;
      OPEN paytran1_cursor;
     v_hours := 0;
      loop
       FETCH paytran1_cursor INTO v_idno1, v_hourswk;
         EXIT WHEN paytran1_cursor%NOTFOUND;
       v_hours := v_hours + v_hourswk;
      dbms_output.put_line('The current amount is: '||v_hours);
         END LOOP;
       INSERT into allinfo1 
          VALUES(v_idno, v_name, v_hours, v_job, v_sal, v_hourswk);
      CLOSE paytran1_cursor;
     END LOOP;
     CLOSE paydata4_cursor;
END;
/
SET SERVEROUTPUT OFF

这些是我的表格

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

8 rows selected.

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

这是我的输出

   SQL> select *
  2  from allinfo1;

IDNO NAME                 HOURS JO        PAY      OTPAY
---- --------------- ---------- -- ---------- ----------
1111 Ann French              45 S        1442         15
2222 Robert Costa            61 H        2025         20
3333 Linda Ames              40 H        3050         20
4444 Scott Brooks            43 S        1500          5
5555 Susan Ash               40 S        1096         40
6666 James Smith             44 S        1058         44
7777 Mary Jones              45 H        1584         15
8888 John Morse              40 H        1755          5

8 rows selected.

预期输出应为

SQL>选择 *       2来自allinfo1;

IDNO NAME                 HOURS JO        PAY      OTPAY
---- --------------- ---------- -- ---------- ----------
1111 Ann French              45 S        1442
2222 Robert Costa            61 H        2745
3333 Linda Ames              40 H        2000
4444 Scott Brooks            43 S        1500
5555 Susan Ash               40 S        1096
6666 James Smith             44 S        1058
7777 Mary Jones              45 H        1620
8888 John Morse              40 H        1560

1 个答案:

答案 0 :(得分:0)

不确定是否需要存储过程。如果我理解你的过程正确,你就是想做这样的事情:

SELECT x.idno, x.name, x.hours, x.j
      ,DECODE(x.j, 'H', x.hours * y.payhr, y.salary/52) AS Pay

  FROM ( SELECT p.idno, p.name
               ,SUM(t.HOURSWK) AS hours
               ,p.j
           FROM paydata1 p
           JOIN paytran1 t ON (t.idno = p.idno)
           GROUP BY p.idno, p.name, p.j
       ) x

   JOIN paydata1 y ON(y.idno = x.idno)
   ORDER BY x.IDNO

输出:

| IDNO |         NAME | HOURS | J |                PAY |
|------|--------------|-------|---|--------------------|
| 1111 |   Ann French |    45 | S | 1442.3076923076924 |
| 2222 | Robert Costa |    61 | H |               2745 |
| 3333 |   Linda Ames |    40 | H |               2000 |
| 4444 | Scott Brooks |    43 | S |               1500 |
| 5555 |    Susan Ash |    40 | S | 1096.1538461538462 |
| 6666 |  James Smith |    44 | S | 1057.6923076923076 |
| 7777 |   Mary Jones |    45 | H |               1620 |
| 8888 |   John Morse |    40 | H |               1560 |