0

我有以下 PL/SQL 函数:

CREATE OR REPLACE FUNCTION GETOVERUREN(v_user_id IN NUMBER, v_jaar IN NUMBER)
RETURN NUMBER
AS
    v_resultaat number := 0;
    v_min_uren_id number := 0;
    v_max_uren_id number := 0;
BEGIN
    SELECT MIN(UREN_ID) INTO v_min_uren_id FROM UREN WHERE JAAR_NR = v_jaar AND UREN_ID IN (SELECT UREN_ID FROM GEBRUIKER_UREN g WHERE g.USER_ID = v_user_id);
    SELECT MAX(UREN_ID) INTO v_max_uren_id FROM UREN WHERE JAAR_NR = v_jaar AND UREN_ID IN (SELECT UREN_ID FROM GEBRUIKER_UREN g WHERE g.USER_ID = v_user_id);

    DECLARE
        v_subtotaal number := 0;
    BEGIN
    v_max_uren_id := v_max_uren_id +1;
    WHILE v_min_uren_id < v_max_uren_id LOOP
        SELECT SUM(OMAANDAG+ODINSDAG +OWOENSDAG +ODONDERDAG +OVRIJDAG +OZATERDAG +OZONDAG) INTO v_subtotaal FROM UREN WHERE UREN_ID = v_min_uren_id;

                    ----------------------------------------------
                    FIXED: v_min_uren_id := v_min_uren_id +1;(FORGOT TO + THE LOOP ITSELF)
                    ----------------------------------------------

                    v_resultaat := v_resultaat + v_subtotaal;
    END LOOP;
RETURN v_resultaat;
END;
END;

The following sql command should give the following outcome:
SELECT GETOVERUREN(1,2013) FROM UREN; WHERE 1 is the userid and 2013 is the year

GETOVERUREN(1,2013)
-------------------
          10.25

But instead it gives:

GETOVERUREN(1,2013)
-------------------
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
          10.25 
 Up to 157 times (wich is the total of rows i have in my table.)

/* 当我使用函数时,SQLDeveloper 保持运行并且不会停止运行该函数。我等了半个小时还是没有结果。我做错了什么?*/

运行问题已解决,现在我得到了很多结果。

我给自己做了一个示例函数,如下所示:

create or replace FUNCTION TESTING(v_user_id IN NUMBER)
RETURN NUMBER
AS
    v_resultaat number := 0;
BEGIN
    SELECT IS_ADMIN INTO v_resultaat FROM GEBRUIKER WHERE USER_ID = v_user_id;
  return v_resultaat;
END TESTING;

这个函数确实返回一个值,但它返回的值 X 是 GEBRUIKER 中的行数,所以再次出现问题,但我似乎无法弄清楚它是什么。

4

1 回答 1

3

您必须在循环v_min_uren_id移动变量的增量(并将其从最大值更改为最小值) - 否则,您会得到一个无限循环。尝试这个:

CREATE OR REPLACE FUNCTION GETOVERUREN(v_user_id IN NUMBER, v_jaar IN NUMBER)
RETURN NUMBER
AS
    v_resultaat number := 0;
    v_min_uren_id number := 0;
    v_max_uren_id number := 0;
BEGIN
    SELECT MIN(UREN_ID) INTO v_min_uren_id FROM UREN WHERE JAAR_NR = v_jaar AND UREN_ID IN (SELECT UREN_ID FROM GEBRUIKER_UREN g WHERE g.USER_ID = v_user_id);
    SELECT MAX(UREN_ID) INTO v_max_uren_id FROM UREN WHERE JAAR_NR = v_jaar AND UREN_ID IN (SELECT UREN_ID FROM GEBRUIKER_UREN g WHERE g.USER_ID = v_user_id);

    DECLARE
        v_subtotaal number := 0;
    BEGIN
    WHILE v_min_uren_id < v_max_uren_id LOOP
        v_min_uren_id := v_min_uren_id +1;
        SELECT SUM(OMAANDAG+ODINSDAG +OWOENSDAG +ODONDERDAG +OVRIJDAG +OZATERDAG +OZONDAG) INTO v_subtotaal FROM UREN WHERE UREN_ID = v_min_uren_id;

        v_resultaat := v_resultaat + v_subtotaal;
    END LOOP;
RETURN v_resultaat;
END;
END;
于 2013-10-25T11:28:23.917 回答