例子:
DECLARE ORDER_NUM VARCHAR2(20);
BEGIN
SELECT S.ORDER_NUM INTO ORDER_NUM FROM SALES_ORDER S WHERE S.ID=122344;
DBMS_OUTPUT.PUT_LINE('单号:' || ORDER_NUM);
END;
在查询结果为空的情况下,以上代码会报错:未找到任何数据
解决方法:
改为 SELECT MAX(S.ORDER_NUM) INTO ORDER_NUM FROM SALES_ORDER S WHERE S.ID=122344;
SELECT S.ORDER_NUM 查不到数据情况下,一条结果也没有;但 SELECT MAX(S.ORDER_NUM) 不管是否查到数据,都会有一条结果。这是这条结果赋值给了变量,这才不会报错。
标签:NO,INTO,NUM,为空,ORDER,SELECT,赋值 From: https://www.cnblogs.com/haicheng92/p/17658186.html