NO_DATA_FOUND, SELECT COUNT(*) INTO var and var IS NULL clarification on triggers
I was reading that a SELECT that doesn't retrieve any data will generate NO_DATA_FOUND.
But for my experience this isn't true because I did a
SELECT COUNT(*) INTO mylocalvar FROM tbl;
on an empty table and it didn't launch NO_DATA_FOUND exception.
Instead I found out that mylocalvar was NULL.
= 0 but it was NULL.
So basically if I need to check if tha开发者_运维知识库t count(*) is = 0 I have to:
IF mylocalvar IS NULL THEN
  --do stuff 
Is this right? Also why in this case NO_DATA_FOUND doesn't apply?
Can I assign mylocalvar a 0 value as default? Maybe in the declare:DECLARE
   mylocalvar NUMBER := 0;
So I can just do IF mylocalvar = 0 THEN ?
Thanks
I don't see why your query would return NULL. It should return exactly one row, that's why the exception is not thrown.
Since your table has no rows, COUNT(*) is 0. This is stored in mylocalvar.
Try the following code, it outputs
0
DECLARE
  mylocalvar  PLS_INTEGER;
BEGIN
  SELECT COUNT(*)
    INTO mylocalvar
    FROM dual
   WHERE 1 = 2;
  dbms_output.put_line(mylocalvar);
END;
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论