当我比较loadid
时,如果null
为loadid = V_LOAD_ID
,我该如何引发例外?
E_NULL_ID
是我当时想要使用的例外。
这是我的程序:
CREATE OR REPLACE PROCEDURE GET_INFO(P_CURDATE DATE)
AS
V_LOAD_ID CARSALES.LOADID%TYPE;
E_NULL_ID EXCEPTION;
BEGIN
-- Get load id from LoadIds table for current date.
SELECT LoadId
INTO V_LOAD_ID
FROM LoadIds
WHERE DateLoad = P_CURDATE;
-- Select brand name and total sales.
SELECT BrandName, SUM(Cost)
FROM CarSales Sales INNER JOIN
CarLables Lables ON Lables.CarBrandId = Sales.CarBrandId
WHERE LoadId = V_LOAD_ID
GROUP BY CarBrandId
EXCEPTION
WHEN E_NULL_ID THEN
DMBS.OUTPUT.PUT_LINE('No loadId found');
END;
答案 0 :(得分:0)
你可能想通过将它包装在begin-exception-end块中来检查你的select中是否没有行(或太多行)?
CREATE OR REPLACE PROCEDURE GET_INFO(P_CURDATE DATE)
AS
V_LOAD_ID CARSALES.LOADID%TYPE;
E_NULL_ID EXCEPTION;
BEGIN
-- Get load id from LoadIds table for current date.
SELECT LoadId
INTO V_LOAD_ID
FROM LoadIds
WHERE DateLoad = P_CURDATE;
IF (V_LOAD_ID IS NULL) THEN
RAISE_APPLICATION_ERROR(-20001,'V_LOAD_ID is null');
END IF;
-- Select brand name and total sales.
SELECT BrandName, SUM(Cost)
-- you need an into here?
FROM CarSales Sales INNER JOIN
CarLables Lables ON Lables.CarBrandId = Sales.CarBrandId
WHERE LoadId = V_LOAD_ID
GROUP BY CarBrandId;
--EXCEPTION
-- WHEN E_NULL_ID THEN
-- DMBS.OUTPUT.PUT_LINE('No loadId found');
END;
答案 1 :(得分:0)
首先,你的select语句缺少into
子句,它应该是:
SELECT BrandName, SUM(Cost)
into l_brand, l_sum
FROM CarSales Sales INNER JOIN
CarLables Lables ON Lables.CarBrandId = Sales.CarBrandId
WHERE LoadId = V_LOAD_ID;
其次,与Oracle中null
的比较总是评估为FALSE,因此检测某些LoadId
为空的唯一方法是明确检查null
:
declare
...
l_nulls number;
begin
...
SELECT count(*) into l_nulls
FROM CarSales Sales INNER JOIN
CarLables Lables ON Lables.CarBrandId = Sales.CarBrandId
WHERE LoadId IS NULL;
if l_nulls > 0 then
raise E_NULL_ID;
end if;
...
end;