1. The following Code creates a top100cur () function, which returns an anonymous cursor.
- -- Drop function top100cur ();
- Create FunctionTop100cur ()ReturnsRefcursorAs$
- Declare
- Abc refcursor;
- Begin
- OpenAbcFor Select*FromPersons limit 100;
- ReturnAbc;
- End
- $ Language plpgsql;
- SELECTTop100cur ();-- Returns an anonymous cursor.
- -- Fetch all from abc; -- anonymous cursor, so this row is incorrect
2. The following code will create a function and return a cursor named abc (Can you find the difference? Yes. Whether the cursor is anonymous depends on whether the cursor variable is bound to the open cursor)
- -- Drop function top100cur ();
- Create FunctionTop100cur ()ReturnsRefcursorAs$
- Declare
- AbcCursor For Select*FromPersons limit 100;
- Begin
- OpenAbc;
- ReturnAbc;
- End
- $ Language plpgsql;
- SELECTTop100cur ();-- Returns a cursor named abc.
- Fetch All FromAbc;-- This row is correct. During execution, remember to drag this row to the blacklist before the select row, and then F5
3. Return the cursor named by the caller.
- -- Drop function top100cur (refcursor );
- Create FunctionTop100cur (refcursor)ReturnsRefcursorAs$
- Begin
- Open$1For Select*FromPersons limit 100;
- Return$1;
- End
- $ Language plpgsql;
- SELECTTop100cur ('Abc');
- Fetch All FromAbc;
4. A cursor named $1 is returned.
- -- Drop function top100cur (refcursor );
- Create FunctionTop100cur (refcursor)ReturnsRefcursorAs$
- Declare
- $1Cursor For Select*FromPersons limit 100;
- Begin
- Open$1;-- If it is not open, the returned name is "$1" and the cursor cannot be used!
- Return$1;
- End
- $ Language plpgsql;
- SELECTTop100cur ('Abc');
- Fetch All From "$1";
5. An anonymous cursor is returned.
- -- Drop function top100cur (refcursor );
- Create FunctionTop100cur (refcursor)ReturnsRefcursorAs$
- Declare
- $1 refcursor;
- Begin
- Open$1For Select*FromPersons limit 100;
- Return$1;
- End
- $ Language plpgsql;
- SELECTTop100cur ('Abc');
- -- Fetch all from "$1"; -- anonymous cursor, so this row is incorrect
6. Return the cursor specified by the caller (Do you know the difference from 4 and 5? In 4 and 5, the cursor variable name declared by declare is too special, and the anonymous parameter name is hidden ......)
- -- Drop function top100cur (refcursor );
- Create FunctionTop100cur (refcursor)ReturnsRefcursorAs$
- Declare
- Abcdef refcursor;
- Begin
- Open$1For Select*FromPersons limit 100;
- Return$1;
- End
- $ Language plpgsql;
- SELECTTop100cur ('Abc');
- Fetch All From "Abc";
7. open a cursor variable that is neither declare nor passed in.
- -- Drop function top100cur (refcursor );
- Create FunctionTop100cur (refcursor)ReturnsRefcursorAs$
- Declare
- Abcdef refcursor;
- Begin
- -- Open defg for select * from person limit 100; -- error: "defg" is not a known variable
- -- Return defg; ----------- LINE 5: open defg for select * from person limit 100;
- End
- $ Language plpgsql;
Summary:
1. declare variables will cause function parameters to be hidden (for example, after declare $1, the first anonymous parameter will be hidden );
2. declare only declares a cursor and does not open the cursor. A cursor without open cannot be used ~
3. When you open the cursor after declare, if the cursor is unbound (no cursor for XXX When declare is used), an anonymous cursor is obtained after the cursor is opened;
4. The cursor variable of the open operation is either declare or a string passed in as a parameter. In addition, an error is returned!