如何绕过INTO语句中的空值

时间:2016-09-17 00:41:53

标签: sql oracle plsql

我在另一个问题中发布了此查询,但这是一个不同的问题。我试图获取此值并将其存储到变量中。我知道我的查询非常复杂,但它确实有效,我只需要知道如果它是空的,如何处理异常。

SELECT DISTINCT *
INTO MUSICIAN_NUM, NUM_ALBUMS
FROM (
SELECT MNO, COUNT(MNO)
FROM PERFORM
WHERE PERFORM.SNO IN (
    SELECT DISTINCT S.ANO
    FROM SONG S
    WHERE S.SNO IN (
        SELECT DISTINCT P.SNO
        FROM PERFORM P
        WHERE j IN P.MNO
        GROUP BY P.SNO
    )
)
AND PERFORM.MNO != SUB_MUSICIAN_NUM
AND PERFORM.MNO != TEMP_MUS_NUM
GROUP BY MNO
ORDER BY COUNT(MNO) DESC
)
WHERE ROWNUM = 1;

某些值将返回任何内容,并尝试存储到MUSICIAN_NUMNUM_ALBUMS

然后我尝试在这里打印出来:

DBMS_OUTPUT.PUT_LINE(MUSICIAN_NUM || '  ' || MUSICIAN_NAME || ' ' || NUM_ALBUMS);

我收到了这个错误:

DECLARE
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 38

1 个答案:

答案 0 :(得分:3)

null与没有数据非常不同。从问题的文本,您似乎正在尝试处理查询不返回任何数据的情况。这看起来像这样。如果没有找到数据,我猜你希望你的两个变量是null,但你可以在异常处理程序中添加你想要的任何内容。

BEGIN
  <<your query>>
EXCEPTION
  WHEN no_data_found
  THEN
    MUSICIAN_NUM := null;
    NUM_ALBUMS := null;
END;