7

I have OUT parameter of a stored procedure as a REF CURSOR. Based on a particular condition, I would want to either return a result set (already implemented).

But how do I return an empty cursor when the condition fails? Without raising an exception? Just pasting pseudo code:

IF condition = true THEN
   OPEN OUT_CUR FOR 
   Select Some query

ELSE

   Return empty OUT_CUR

END IF
General Grievance
  • 4,555
  • 31
  • 31
  • 45
KeenUser
  • 5,305
  • 14
  • 41
  • 62

2 Answers2

18

you can try this

IF condition = true THEN
   OPEN OUT_CUR FOR 
   Select Some query;
ELSE
   OPEN OUT_CUR FOR 
       Select * from mtable where 1=2;
END IF
return OUT_CUR;
Thor
  • 498
  • 1
  • 6
  • 15
Satya
  • 8,693
  • 5
  • 34
  • 55
1
IF condition = true THEN
   OPEN OUT_CUR FOR 
   Select Some query

ELSE

    OPEN OUT_CUR FOR select * from unnest(array[1,2]) arr where  false 

END IF
Jared Forth
  • 1,577
  • 6
  • 17
  • 32
  • 1
    So far as I can tell, the only difference between this and @Satya's proposal from eight years ago is that you use `unnest(array[1,2]) arr where false` whereas Satya uses `mtable where 1=2`. Can you edit your answer to discuss why your approach might be preferable to Satya's well-established answer? – Jeremy Caney May 28 '20 at 18:49
  • 1
    because no need any table for return empty cursor. – Vusal Hasanli Jun 21 '20 at 10:35