title: Fetching rows from a cursor PostgreSQL
body: <p>I have the cursor code:</p>
  
  <pre><code>BEGIN;
  DECLARE cliente_cursor 
  CURSOR FOR SELECT * FROM cliente;
  </code></pre>
  
  <p>I want to read all the content from the Table 'cliente':</p>
  
  <p><img src="https://i.stack.imgur.com/UCqTh.png" alt="Table cliente"></p>
  
  <p>With the use of a cursor. 
  I have the code working for SQL Server:</p>
  
  <pre><code>DECLARE cliente_cursor CURSOR
        FOR SELECT * FROM cliente
  OPEN cliente_cursor
  FETCH NEXT FROM cliente_cursor;
  While @@FETCH_STATUS=0
  BEGIN
       FETCH NEXT FROM cliente_cursor;
  End
  CLOSE cliente_cursor
  DEALLOCATE cliente_cursor
  </code></pre>
  
  <p>And I want to have a working code for PostgreSQL.</p>
  
  <p>I have been looking for a solution &amp; seen people usually suggest using functions. I wonder if there is any way in this PostgreSQL DBMS to create something similar to the code in SQL Server.</p>
  
  <p>I had written this code:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION MyFunction()
  RETURNS setof cliente AS $$
  DECLARE 
  cursor_cliente CURSOR FOR SELECT * FROM cliente;
  rec cliente%ROWTYPE;
   BEGIN
   OPEN cursor_cliente;
  loop
  --fetch the table row inside the loop
  FETCH cursor_cliente INTO rec;
  -- check if there is no record
     --exit from loop when record not found
     if not found then
          exit ;
     end if;
  end loop;
  RETURN;
  END;
  $$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>But when I run it, I only get: </p>
  
  <pre><code>select MyFunction();
  </code></pre>
  
  <p>Any idea what should the code be instead?</p>
  
  <p><img src="https://i.stack.imgur.com/ZPpPA.png" alt="Results"></p>
  
  <p>Any help would be appreciated a lot!</p>