我目前正在尝试编写一个PL / SQL脚本,该脚本将从我的saleinv表中收集与用户输入的序列号相匹配的信息。目前,我的代码返回错误错误消息是ORA-01422:精确提取返回超过请求的行数。如果有人可以向我解释如何解决问题,我们将非常感激。我的代码如下。
ACCEPT p_saleinv PROMPT 'Enter sales invoice number: '
VARIABLE g_output VARCHAR2(4000)
DECLARE
v_ABC NUMBER(4);
v_DEF EXCEPTION;
v_freightcost car.freightcost%TYPE;
v_totalcost car.totalcost%TYPE;
CURSOR saleinvoicelist IS
SELECT *
FROM saleinv
WHERE saleinv = '&p_saleinv';
BEGIN
SELECT COUNT(*)
INTO v_ABC
FROM saleinv
WHERE saleinv = '&p_saleinv';
SELECT freightcost,totalcost
INTO v_freightcost,v_totalcost
FROM car c, saleinv si
WHERE c.serial = si.serial
AND c.cname = si.cname;
IF v_ABC = '0' THEN
RAISE v_DEF;
ELSE
FOR v_saleinv IN saleinvoicelist LOOP
:g_output := :g_output||TRIM(v_saleinv.saleinv)||' ';
:g_output := :g_output||TRIM(v_saleinv.cname)||' ';
:g_output := :g_output||TRIM(v_saleinv.saledate)||' ';
:g_output := :g_output||TRIM(v_saleinv.serial)||' ';
:g_output := :g_output||TO_CHAR(v_freightcost,'122345.69')||CHR(10);
:g_output := :g_output||TO_CHAR(v_saleinv.tax,'12345.99')||' ';
:g_output := :g_output||TO_CHAR(v_saleinv.licfee,'123.76')||' ';
:g_output := :g_output||TO_CHAR(v_saleinv.commission,'1234.59')||' ';
:g_output := :g_output||TO_CHAR(v_totalcost,'123489.97')||CHR(10);
:g_output := :g_output||TO_CHAR(v_saleinv.totalprice,'12345.45')||' ';
END LOOP;
END IF;
EXCEPTION
WHEN v_DEF THEN
ROLLBACK WORK;
:g_output := 'Such data does not exist.';
WHEN OTHERS THEN
ROLLBACK WORK;
-- check error message
:g_output := 'Error'||CHR(10);
:g_output := :g_output||'Error code is '||SQLCODE||CHR(10);
:g_output := :g_output||'Error message is '||SQLERRM;
END;
/
PRINT g_output
答案 0 :(得分:0)
此错误的最可能原因是以下选择
SELECT freightcost,totalcost
INTO v_freightcost,v_totalcost
FROM car c, saleinv si
WHERE c.serial = si.serial
AND c.cname = si.cname;
因为您正在选择变量,所以select只能返回一行。我假设您使用的序列号将从此选择返回多行。您需要确保它返回一个唯一的行。
答案 1 :(得分:0)
SELECT INTO语句下面只需要奇异变量中的单个输出。您需要处理异常或修改代码,如果您期望更多输出,则使用FOR LOOP。 希望在片段下方有所帮助。
ACCEPT p_saleinv PROMPT 'Enter sales invoice number: '
VARIABLE g_output VARCHAR2(4000)
DECLARE
v_ABC NUMBER(4);
v_DEF EXCEPTION;
v_freightcost car.freightcost%TYPE;
v_totalcost car.totalcost%TYPE;
CURSOR saleinvoicelist
IS
SELECT * FROM saleinv WHERE saleinv = '&p_saleinv';
BEGIN
SELECT COUNT(*) INTO v_ABC FROM saleinv WHERE saleinv = '&p_saleinv';
BEGIN
SELECT freightcost,
totalcost
INTO v_freightcost,
v_totalcost
FROM car c,
saleinv si
WHERE c.serial = si.serial
AND c.cname = si.cname;
EXCEPTION
WHEN TOO_MANY_ROWS THEN
dbms_output.put_line('Too many rows fetched');
RETURN;
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('No rows fetched');
RETURN;
IF v_ABC = '0' THEN
RAISE v_DEF;
ELSE
FOR v_saleinv IN saleinvoicelist
LOOP
:g_output := :g_output||TRIM(v_saleinv.saleinv)||' ';
:g_output := :g_output||TRIM(v_saleinv.cname)||' ';
:g_output := :g_output||TRIM(v_saleinv.saledate)||' ';
:g_output := :g_output||TRIM(v_saleinv.serial)||' ';
:g_output := :g_output||TO_CHAR(v_freightcost,'122345.69')||CHR(10);
:g_output := :g_output||TO_CHAR(v_saleinv.tax,'12345.99')||' ';
:g_output := :g_output||TO_CHAR(v_saleinv.licfee,'123.76')||' ';
:g_output := :g_output||TO_CHAR(v_saleinv.commission,'1234.59')||' ';
:g_output := :g_output||TO_CHAR(v_totalcost,'123489.97')||CHR(10);
:g_output := :g_output||TO_CHAR(v_saleinv.totalprice,'12345.45')||' ';
END LOOP;
END IF;
EXCEPTION
WHEN v_DEF THEN
ROLLBACK WORK;
:g_output := 'Such data does not exist.';
WHEN OTHERS THEN
ROLLBACK WORK;
-- check error message
:g_output := 'Error'||CHR(10);
:g_output := :g_output||'Error code is '||SQLCODE||CHR(10);
:g_output := :g_output||'Error message is '||SQLERRM;
END;
/