title: TSQL cursor translation to PostgreSQL "query has no destination for result"
body: <p>I inherited some code that was originally in TSQL that was poorly translated into PostgreSQL.  I am tasked with getting the code to work after the contractor did a terrible job of translation.  I don't know if the first insert loop could be rewritten to be a straight forward insert.  Right now it sort of works.  Anyway...</p>
  <p>This code is intended to be run as one block against a Postgres database using SSRS.  When I run the entire code block against the database in DBeaver I get:</p>
  <p><code>SQL Error [42601]: ERROR: query has no destination for result data  Hint: If you want to discard the results of a SELECT, use PERFORM instead. Where: PL/pgSQL function inline_code_block line 40 at SQL statement</code></p>
  <p>If I run the create table and loop/cursor it first and then run the select it returns a result.  Is there a way to run it all at once without using a stored proc or function?  Code below:</p>
  <pre><code>do
  $$
      declare
          _currentRow  record;
          _refcur      refcursor;
      begin
          drop table if exists &quot;allActionItemsWithTempSalesPersons&quot;;
          create temp table &quot;allActionItemsWithTempSalesPersons&quot;  --select * from &quot;allActionItemsWithTempSalesPersons&quot;
          (
              &quot;ID&quot;              int,
              &quot;SalesPersonID&quot;   int,
              &quot;CustomerID&quot;      int,
              &quot;ActionTypeID&quot;    int,
              &quot;Subject&quot;         text,
              &quot;ActionDate&quot;      timestamp without time zone,
              &quot;DurationMintues&quot; int,
              &quot;PriorityID&quot;      int,
              &quot;ResultTypeID&quot;    int,
              &quot;Note&quot;            text,
              &quot;StageId&quot;         int,
              &quot;ActionCloseDate&quot; timestamp without time zone,
              &quot;LUEmployeeID&quot;    int,
              &quot;LUDate&quot;          timestamp without time zone
          );
  --Get all action items with temp sales persons handled
          call dbo.&quot;HF_SalesPersonActionList_GetActionItemsBySalesPersonId&quot;('0', '3', _refcur);
          loop
              fetch from _refcur into _currentRow;
              EXIT WHEN NOT found;
              INSERT INTO &quot;allActionItemsWithTempSalesPersons&quot;(&quot;ID&quot;, &quot;SalesPersonID&quot;, &quot;CustomerID&quot;, &quot;ActionTypeID&quot;,
                                                                &quot;Subject&quot;, &quot;ActionDate&quot;, &quot;DurationMintues&quot;, &quot;PriorityID&quot;,
                                                                &quot;ResultTypeID&quot;, &quot;Note&quot;, &quot;StageId&quot;, &quot;ActionCloseDate&quot;,
                                                                &quot;LUEmployeeID&quot;, &quot;LUDate&quot;)
              VALUES (_currentRow.&quot;ID&quot;, _currentRow.&quot;SalesPersonID&quot;, _currentRow.&quot;CustomerID&quot;, _currentRow.&quot;ActionTypeID&quot;,
                      _currentRow.&quot;Subject&quot;, _currentRow.&quot;ActionDate&quot;, _currentRow.&quot;DurationMintues&quot;,
                      _currentRow.&quot;PriorityID&quot;, _currentRow.&quot;ResultTypeID&quot;, _currentRow.&quot;Note&quot;,
                      _currentRow.&quot;StageId&quot;, _currentRow.&quot;ActionCloseDate&quot;, _currentRow.&quot;LUEmployeeID&quot;,
                      _currentRow.&quot;LUDate&quot;);
          end loop;
  
          select Emp.&quot;FirstName&quot; || ' ' || Emp.&quot;LastName&quot;       AS &quot;SalesPerson&quot;,
                 coalesce(&quot;Club&quot;.&quot;Name&quot;, '')                    AS &quot;Club&quot;,
                 &quot;Customer&quot;.&quot;DisplayName&quot;                       AS &quot;Name&quot;,
                 SAL.&quot;ActionDate&quot;                               AS &quot;ActionDate&quot;,
                 CASE
                     WHEN &quot;Customer&quot;.&quot;IsLead&quot; THEN 'Lead'
                     ELSE dbo.&quot;HF_fn_GetMemberType&quot;(&quot;Customer&quot;.&quot;MemberType&quot;)
                     END                                        AS &quot;MemberType&quot;,
                 act.&quot;Description&quot;                              AS &quot;Action&quot;,
                 SAL.&quot;Subject&quot;                                  AS &quot;Subject&quot;,
                 coalesce(&quot;Priority&quot;.&quot;Description&quot;, '')         AS &quot;Priority&quot;,
                 coalesce(&quot;Stages&quot;.&quot;Description&quot;, '')           AS &quot;Stage&quot;,
                 coalesce(&quot;Source&quot;.&quot;Description&quot;, '')           AS &quot;Source&quot;,
                 (CASE &quot;Customer&quot;.&quot;PreferredPhone&quot;
                      WHEN 'M' THEN &quot;Customer&quot;.&quot;CellPhone&quot;
                      WHEN 'W' THEN &quot;Customer&quot;.&quot;WorkPhone&quot;
                      WHEN 'H' THEN &quot;Customer&quot;.&quot;HomePhone&quot; END) AS &quot;PreferredPhone&quot;,
                 coalesce (&quot;Customer&quot;.&quot;Email&quot;, '')              AS &quot;Email&quot;,
                 &quot;Customer&quot;.&quot;ID&quot;                                AS &quot;CustomerId&quot;,
                 &quot;Customer&quot;.&quot;BarcodeID&quot;                         AS &quot;MemberId&quot;,
                 coalesce(RT.&quot;Description&quot;, '')                 AS &quot;Result&quot;,
                 SAL.&quot;Note&quot;                                     AS &quot;Notes&quot;
          FROM &quot;allActionItemsWithTempSalesPersons&quot; SAL
                   INNER JOIN dbo.&quot;Action&quot; act         ON SAL.&quot;ActionTypeID&quot; = act.&quot;ID&quot;
                   INNER JOIN dbo.&quot;Employee&quot; Emp       ON SAL.&quot;SalesPersonID&quot; = Emp.&quot;ID&quot;
                   INNER JOIN dbo.&quot;Customer&quot;           ON SAL.&quot;CustomerID&quot; = &quot;Customer&quot;.&quot;ID&quot;
                   LEFT OUTER JOIN dbo.&quot;ResultType&quot; RT ON SAL.&quot;ResultTypeID&quot; = RT.&quot;ID&quot;
                   LEFT OUTER JOIN dbo.&quot;Club&quot;          ON &quot;Club&quot;.&quot;ID&quot; = &quot;Customer&quot;.&quot;ClubNumber&quot;
                   LEFT OUTER JOIN dbo.&quot;Stages&quot;        ON &quot;Customer&quot;.&quot;StageId&quot; = &quot;Stages&quot;.&quot;ID&quot;
                   LEFT OUTER JOIN dbo.&quot;Priority&quot;      ON SAL.&quot;PriorityID&quot; = &quot;Priority&quot;.&quot;PriorityID&quot;
                   LEFT OUTER JOIN dbo.&quot;Source&quot;        ON &quot;Customer&quot;.&quot;SourceID&quot; = &quot;Source&quot;.&quot;ID&quot;
          WHERE SAL.&quot;ActionDate&quot; &gt;= now()::date
            AND SAL.&quot;ActionDate&quot; &lt; (now() + interval '2 day')::date
            AND &quot;Customer&quot;.&quot;ClubNumber&quot; = 2 /* use 2 for debugging ? --@clubId */
          ORDER BY SAL.&quot;ActionDate&quot;, 
                   Emp.&quot;FirstName&quot; || ' ' || Emp.&quot;LastName&quot;;
      end;
  $$
  </code></pre>
  <p>Any help would be appreciated.</p>