如何返回expirydate-sysdate
的值?它给了我错误。任何人都可以帮助我吗?
CREATE OR REPLACE FUNCTION calc_remain_data(accountNumber account.account_number%TYPE)
RETURN date IS
CridetCardNo number(8);
expirydate date;
res date;
BEGIN
SELECT credit_card_number into CridetCardNo
FROM c_relation
WHERE account_number = accountNumber ;
SELECT expiry_date into expirydate
FROM credit_card
WHERE credit_card_number = CridetCardNo;
SELECT expirydate-SYSdate as result
from dual ;
return result ;
END;
答案 0 :(得分:3)
您不需要两个不同的SELECT语句,只需一个JOIN即可更有效地完成。
从另一个日期中减去一个日期的结果不是日期,而是表示这两个日期之间的天数的数字。因此,您无法将l_expirydate - sysdate
的结果分配给定义为DATE
的变量。
您没有告诉我们该函数究竟应该返回什么。如果“剩余数据”意味着天,则将l_result定义为number
并更改函数以返回数字:
CREATE OR REPLACE FUNCTION calc_remain_data(accountNumber account.account_number%TYPE)
RETURN number --<< HERE
IS
l_remaining_days number; --<< NOT a date!
BEGIN
-- you can store the result of the calculation directly in the variable
SELECT cc.expiry_date - sysdate
INTO l_remaining_days
FROM c_relation cr
JOIN credit_card cc ON cc.credit_card_number = cr.credit_card_number
WHERE account_number = accountNumber;
return l_remaining_days;
END;
/
答案 1 :(得分:1)
你可以用这个
CREATE OR REPLACE FUNCTION calc_remain_data(accountNumber account.account_number%TYPE)
RETURN number IS
l_CreditCardNo number(8);
l_expirydate date;
l_result number;
BEGIN
SELECT credit_card_number INTO l_CreditCardNo
FROM c_relation
WHERE account_number = accountNumber ;
SELECT expiry_date INTO l_expirydate
FROM credit_card
WHERE credit_card_number = l_CreditCardNo;
l_result := l_expirydate - sysdate;
return l_result ;
END;
编辑:更改返回类型为注释
答案 2 :(得分:0)
您可以使用 INTERVAL 数据类型:
CREATE OR REPLACE FUNCTION CALC_REMAIN_DATA (
ACCOUNTNUMBER ACCOUNT.ACCOUNT_NUMBER%TYPE)
RETURN INTERVAL DAY TO SECOND--You can specify desired type here
IS
CRIDETCARDNO NUMBER (8);
EXPIRYDATE DATE;
RES DATE;
BEGIN
SELECT CREDIT_CARD_NUMBER
INTO CRIDETCARDNO
FROM C_RELATION
WHERE ACCOUNT_NUMBER = ACCOUNTNUMBER;
SELECT EXPIRY_DATE
INTO EXPIRYDATE
FROM CREDIT_CARD
WHERE CREDIT_CARD_NUMBER = CRIDETCARDNO;
RETURN NUMTODSINTERVAL (EXPIRYDATE - SYSDATE, 'Day');--this function convert float number to interval type and has another version numtoyminterval
--^this specify the unit to calculate the interval
END;
实际日期类型在这里不会对你有所帮助,而是间隔类型有助于在{Day Hour Minute Seconds Timestamp}中获得两个日期的差值,或者你可以使用另一个Interval类型Year to Month来计算{Year Month}中的值< / p>