我有以下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中的行数,这是错误的但我似乎无法弄清楚它是什么。
答案 0 :(得分:3)
你必须在循环中移动v_min_uren_id
变量的增量(并将其从max更改为min) - 否则,你会得到一个无限循环。试试这个:
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;