我写了一个PL / SQL函数,它按员工ID更新工资,并将秒表作为INTERVAL DAY TO SECOND返回。
当被叫时,它会更新薪水,但我也希望它打印出秒表值。如何实现?我已经尝试添加此行dbms_output.put_line(stopwatch);
,但该功能仍然只是更新工资,并且不会打印出秒表值。
CREATE OR REPLACE function myfunction1(e_empno_in IN NUMBER, e_sal_in IN NUMBER)
RETURN INTERVAL DAY TO SECOND
IS
e_empno emp.empno%TYPE := e_empno_in;
e_sal emp.sal%TYPE := e_sal_in;
thestart TIMESTAMP;
stopwatch INTERVAL DAY TO SECOND;
BEGIN
SELECT current_timestamp INTO thestart FROM dual;
UPDATE emp SET sal = e_sal WHERE empno = e_empno;
IF SQL%ROWCOUNT = 0 THEN
raise_application_error (-10001, 'No row found for id ' || e_empno);
END IF;
SELECT current_timestamp-thestart INTO stopwatch FROM dual;
RETURN stopwatch;
dbms_output.put_line(stopwatch); --> *trying to print out stopwatch value*
END;
调用该函数:
BEGIN
dbms_output.put_line(myfunction1(7654, 2559));
END;
答案 0 :(得分:2)
您需要DBMS_OUTPUT.PUT_LINE
语句之前的RETURN
。
您也不需要使用SQL来获取当前时间(可以在PL / SQL中完成),并且您不需要重新分配输入参数以将它们用作绑定变量。
CREATE function myfunction1(
e_empno_in IN emp.empno%TYPE,
e_sal_in IN emp.sal%TYPE
)
RETURN INTERVAL DAY TO SECOND
IS
thestart TIMESTAMP := CURRENT_TIMESTAMP;
stopwatch INTERVAL DAY TO SECOND;
BEGIN
UPDATE emp
SET sal = e_sal_in
WHERE empno = e_empno_in;
IF SQL%ROWCOUNT = 0 THEN
raise_application_error (-10001, 'No row found for id ' || e_empno_in );
END IF;
stopwatch := CURRENT_TIMESTAMP - thestart;
DBMS_OUTPUT.PUT_LINE(stopwatch);
RETURN stopwatch;
END;
/