我希望我的功能显示“找到空发票!”#39;或者'未找到发票'错误消息。 但是当我选择有错误的数据时(表中不存在),它返回“没有选择行”#39;请指导我选择正确的方法。
SET ECHO ON;
SET SERVEROUTPUT ON;
CREATE OR REPLACE FUNCTION totalInvoiceAmount(InvoiceNo IN INVOICE_ITEM.InvoiceNumber%TYPE)
RETURN NUMBER IS
TotalCost NUMBER(20);
CURSOR X_RECORD IS SELECT * FROM INVOICE_ITEM;
CURSOR Y_RECORD IS SELECT * FROM INVOICE_ITEM;
tempCost NUMBER:=0;
InvoiceCheck BOOLEAN:=FALSE;
InvoiceEmpty BOOLEAN:=TRUE;
BEGIN
FOR Record IN X_RECORD LOOP
IF Record.InvoiceNumber = InvoiceNo THEN
InvoiceCheck:=TRUE;
EXIT;
ELSE
InvoiceCheck:=FALSE;
END IF;
END LOOP;
FOR Record IN Y_RECORD LOOP
IF Record.InvoiceNumber = InvoiceNo AND InvoiceCheck=FALSE THEN
InvoiceEmpty:=TRUE;
EXIT;
ELSE
InvoiceEmpty:=FALSE;
END IF;
END LOOP;
IF InvoiceCheck=FALSE AND InvoiceEmpty=FALSE THEN
DBMS_OUTPUT.PUT_LINE('Invoice Not Found!');
RETURN -1;
ELSIF InvoiceCheck=FALSE AND InvoiceEmpty=TRUE THEN
DBMS_OUTPUT.PUT_LINE('Empty Invoice Found!');
RETURN 0;
ELSE
FOR Record IN X_RECORD LOOP
IF Record.InvoiceNumber = InvoiceNo THEN
tempCost:=tempCost+(Record.UnitPrice*Record.Quantity);
END IF;
END LOOP;
TotalCost:=tempCost;
RETURN TotalCost;
END IF;
END;
/
SELECT invoicenumber,totalInvoiceAmount(invoicenumber) AS "Total Cost" FROM INVOICE_ITEM Where invoicenumber=2011011;
SELECT invoicenumber,totalInvoiceAmount(invoicenumber) AS "Total Cost" FROM INVOICE_ITEM where invoicenumber=2011010;
答案 0 :(得分:3)
您想要的消息来自您在SELECT语句中调用的函数。因此,如果SELECT没有返回任何行,则没有可用于调用该函数的行。
我担心你不清楚自己想要达到的目标,但无论如何,你需要一种不同的方法。说过我建议这样的东西可能会给你你想要的东西(以更正统和更有效的方式):
SELECT invoicenumber,
sum(UnitPrice*Quantity) AS "Total Cost"
FROM INVOICE_ITEM
where invoicenumber=2011010
group by InvoiceNumber;
答案 1 :(得分:0)
SET ECHO ON;
SET SERVEROUTPUT ON;
CREATE OR REPLACE FUNCTION totalInvoiceAmount(InvoiceNo IN INVOICE_ITEM.InvoiceNumber%TYPE)
RETURN NUMBER IS
TotalCost NUMBER(20);
invoice_not_found EXCEPTION ;
empty_invoice_found EXCEPTION ;
CURSOR X_RECORD IS SELECT * FROM INVOICE_ITEM;
CURSOR Y_RECORD IS SELECT * FROM INVOICE_ITEM;
tempCost NUMBER:=0;
InvoiceCheck BOOLEAN:=FALSE;
InvoiceEmpty BOOLEAN:=TRUE;
BEGIN
FOR Record IN X_RECORD LOOP
IF Record.InvoiceNumber = InvoiceNo THEN
InvoiceCheck:=TRUE;
EXIT;
ELSE
InvoiceCheck:=FALSE;
END IF;
END LOOP;
FOR Record IN Y_RECORD LOOP
IF Record.InvoiceNumber = InvoiceNo AND InvoiceCheck=FALSE THEN
InvoiceEmpty:=TRUE;
EXIT;
ELSE
InvoiceEmpty:=FALSE;
END IF;
END LOOP;
IF InvoiceCheck=FALSE AND InvoiceEmpty=FALSE THEN
--DBMS_OUTPUT.PUT_LINE('Invoice Not Found!');
RAISE invoice_not_found;
--RETURN -1;
ELSIF InvoiceCheck=FALSE AND InvoiceEmpty=TRUE THEN
--DBMS_OUTPUT.PUT_LINE('Empty Invoice Found!');
RAISE empty_invoice_found
RETURN 0;
ELSE
FOR Record IN X_RECORD LOOP
IF Record.InvoiceNumber = InvoiceNo THEN
tempCost:=tempCost+(Record.UnitPrice*Record.Quantity);
END IF;
END LOOP;
TotalCost:=tempCost;
RETURN TotalCost;
END IF;
EXCEPTION
when invoice_not_found then
DBMS_OUTPUT.PUT_LINE('Invoice Not Found!');
raise -1;
when empty_invoice_found then
DBMS_OUTPUT.PUT_LINE('Empty Invoice Found!');
raise 0;
END;
您可以在异常中捕获它并相应地处理它,让我知道它是否有效