<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Execute statement in the loop in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaa-p/13835880#M4866723</link>
    <description>&lt;P&gt;That is an excellent answer that solved my problem in a split second. Thank you!&lt;/P&gt;</description>
    <pubDate>Thu, 30 Aug 2012 12:47:17 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2012-08-30T12:47:17Z</dc:date>
    <item>
      <title>Execute statement in the loop</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaq-p/13835877</link>
      <description>&lt;P&gt;Is there any reason why such a block of code wouldn't work? I'm trying to grant permissions to a particular user on multiple tables. The results of it is "Cursor not open". Ideas?&lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="k"&gt;BEGIN&lt;/SPAN&gt;
 &lt;SPAN class="n"&gt;DECLARE&lt;/SPAN&gt; &lt;SPAN class="n"&gt;tabl_name&lt;/SPAN&gt; &lt;SPAN class="n"&gt;CHAR&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;128&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
 &lt;SPAN class="n"&gt;DECLARE&lt;/SPAN&gt; &lt;SPAN class="n"&gt;sql&lt;/SPAN&gt; &lt;SPAN class="n"&gt;CHAR&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;128&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
 &lt;SPAN class="n"&gt;DECLARE&lt;/SPAN&gt; &lt;SPAN class="n"&gt;cur_employee&lt;/SPAN&gt; &lt;SPAN class="n"&gt;CURSOR&lt;/SPAN&gt; &lt;SPAN class="n"&gt;FOR&lt;/SPAN&gt; &lt;SPAN class="nb"&gt;select&lt;/SPAN&gt; &lt;SPAN class="n"&gt;table_name&lt;/SPAN&gt; &lt;SPAN class="n"&gt;from&lt;/SPAN&gt; &lt;SPAN class="n"&gt;systab&lt;/SPAN&gt; &lt;SPAN class="n"&gt;where&lt;/SPAN&gt; &lt;SPAN class="n"&gt;table_name&lt;/SPAN&gt; &lt;SPAN class="n"&gt;like&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'%_ST_VIEW'&lt;/SPAN&gt; &lt;SPAN class="n"&gt;OR&lt;/SPAN&gt; &lt;SPAN class="n"&gt;table_name&lt;/SPAN&gt; &lt;SPAN class="n"&gt;like&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'%_CFG_VIEW'&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;

 &lt;SPAN class="n"&gt;OPEN&lt;/SPAN&gt; &lt;SPAN class="n"&gt;cur_employee&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
 &lt;SPAN class="n"&gt;LOOP&lt;/SPAN&gt;
 &lt;SPAN class="n"&gt;FETCH&lt;/SPAN&gt; &lt;SPAN class="n"&gt;NEXT&lt;/SPAN&gt; &lt;SPAN class="n"&gt;cur_employee&lt;/SPAN&gt; &lt;SPAN class="n"&gt;into&lt;/SPAN&gt; &lt;SPAN class="n"&gt;tabl_name&lt;/SPAN&gt; &lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
     &lt;SPAN class="n"&gt;SET&lt;/SPAN&gt; &lt;SPAN class="n"&gt;sql&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="s"&gt;'GRANT SELECT ON '&lt;/SPAN&gt; &lt;SPAN class="o"&gt;+&lt;/SPAN&gt; &lt;SPAN class="n"&gt;tabl_name&lt;/SPAN&gt; &lt;SPAN class="o"&gt;+&lt;/SPAN&gt; &lt;SPAN class="s"&gt;' TO ASW_Service_RO'&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
     &lt;SPAN class="n"&gt;EXECUTE&lt;/SPAN&gt; &lt;SPAN class="n"&gt;immediate&lt;/SPAN&gt; &lt;SPAN class="n"&gt;sql&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
 &lt;SPAN class="k"&gt;END&lt;/SPAN&gt; &lt;SPAN class="n"&gt;LOOP&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
 &lt;SPAN class="n"&gt;CLOSE&lt;/SPAN&gt; &lt;SPAN class="n"&gt;cur_employee&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="k"&gt;END&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 30 Aug 2012 12:25:12 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaq-p/13835877</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-08-30T12:25:12Z</dc:date>
    </item>
    <item>
      <title>Re: Execute statement in the loop</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaa-p/13835878#M4866721</link>
      <description>&lt;P&gt;You need to use the "WITH HOLD" clause on the OPEN cursor command.&lt;/P&gt;
&lt;P&gt;The GRANT command performs a COMMIT as a side effect, and your cursor is closed on commit without the "WITH HOLD" clause.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Aug 2012 12:28:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaa-p/13835878#M4866721</guid>
      <dc:creator>regdomaratzki</dc:creator>
      <dc:date>2012-08-30T12:28:49Z</dc:date>
    </item>
    <item>
      <title>Re: Execute statement in the loop</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaa-p/13835880#M4866723</link>
      <description>&lt;P&gt;That is an excellent answer that solved my problem in a split second. Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 30 Aug 2012 12:47:17 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaa-p/13835880#M4866723</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-08-30T12:47:17Z</dc:date>
    </item>
    <item>
      <title>Re: Execute statement in the loop</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaa-p/13835879#M4866722</link>
      <description>&lt;P&gt;A even simpler method would be to use a &lt;A href="http://dcx.sybase.com/index.html#1201/en/dbreference/for-statement.html"&gt;FOR loop&lt;/A&gt; - which does open the cursor WITH HOLD as a side effect. Personally, I prefer this over the explicit statements to declare/open/fetch/close cursors (and declaring the fetch variables) since FOR does it all in a combined fashion. - Therefore it does require only half as much SQL code...&lt;/P&gt;
&lt;DIV class="codehilite"&gt;&lt;PRE&gt;&lt;SPAN class="k"&gt;BEGIN&lt;/SPAN&gt;
  &lt;SPAN class="k"&gt;DECLARE&lt;/SPAN&gt; &lt;SPAN class="k"&gt;sql&lt;/SPAN&gt; &lt;SPAN class="nb"&gt;CHAR&lt;/SPAN&gt;&lt;SPAN class="p"&gt;(&lt;/SPAN&gt;&lt;SPAN class="mi"&gt;128&lt;/SPAN&gt;&lt;SPAN class="p"&gt;);&lt;/SPAN&gt;
  &lt;SPAN class="k"&gt;FOR&lt;/SPAN&gt; &lt;SPAN class="n"&gt;for_employee&lt;/SPAN&gt; &lt;SPAN class="k"&gt;AS&lt;/SPAN&gt; &lt;SPAN class="n"&gt;cur_employee&lt;/SPAN&gt; &lt;SPAN class="k"&gt;CURSOR&lt;/SPAN&gt;
    &lt;SPAN class="k"&gt;FOR&lt;/SPAN&gt; &lt;SPAN class="k"&gt;select&lt;/SPAN&gt; &lt;SPAN class="k"&gt;table_name&lt;/SPAN&gt; &lt;SPAN class="k"&gt;as&lt;/SPAN&gt; &lt;SPAN class="n"&gt;tabl_name&lt;/SPAN&gt; &lt;SPAN class="k"&gt;from&lt;/SPAN&gt; &lt;SPAN class="n"&gt;systab&lt;/SPAN&gt;
      &lt;SPAN class="k"&gt;where&lt;/SPAN&gt; &lt;SPAN class="k"&gt;table_name&lt;/SPAN&gt; &lt;SPAN class="k"&gt;like&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'%_ST_VIEW'&lt;/SPAN&gt; &lt;SPAN class="k"&gt;OR&lt;/SPAN&gt; &lt;SPAN class="k"&gt;table_name&lt;/SPAN&gt; &lt;SPAN class="k"&gt;like&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'%_CFG_VIEW'&lt;/SPAN&gt;
  &lt;SPAN class="k"&gt;DO&lt;/SPAN&gt;
     &lt;SPAN class="k"&gt;SET&lt;/SPAN&gt; &lt;SPAN class="k"&gt;sql&lt;/SPAN&gt; &lt;SPAN class="o"&gt;=&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;'GRANT SELECT ON '&lt;/SPAN&gt; &lt;SPAN class="o"&gt;+&lt;/SPAN&gt; &lt;SPAN class="n"&gt;tabl_name&lt;/SPAN&gt; &lt;SPAN class="o"&gt;+&lt;/SPAN&gt; &lt;SPAN class="s1"&gt;' TO ASW_Service_RO'&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
     &lt;SPAN class="k"&gt;EXECUTE&lt;/SPAN&gt; &lt;SPAN class="k"&gt;immediate&lt;/SPAN&gt; &lt;SPAN class="k"&gt;sql&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
 &lt;SPAN class="k"&gt;END&lt;/SPAN&gt; &lt;SPAN class="k"&gt;FOR&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="k"&gt;END&lt;/SPAN&gt;&lt;SPAN class="p"&gt;;&lt;/SPAN&gt;
&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 30 Aug 2012 17:14:30 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaa-p/13835879#M4866722</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2012-08-30T17:14:30Z</dc:date>
    </item>
    <item>
      <title>Re: Execute statement in the loop</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaa-p/13835881#M4866724</link>
      <description>&lt;P&gt;It worked just fine only...never stopped. Does this cursor need to be closed in a special manner?&lt;/P&gt;</description>
      <pubDate>Fri, 31 Aug 2012 04:15:34 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaa-p/13835881#M4866724</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2012-08-31T04:15:34Z</dc:date>
    </item>
    <item>
      <title>Re: Execute statement in the loop</title>
      <link>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaa-p/13835882#M4866725</link>
      <description>&lt;P&gt;Just realized you don't have an exit condition in your loop.  Try something like :&lt;/P&gt;
&lt;PRE&gt;OPEN cur_employee WITH HOLD;
FETCH FIRST cur_employee INTO tabl_name;
WHILE sqlcode = 0 LOOP
  // do something
  FETCH NEXT cur_employee INTO tabl_name;
END LOOP;
CLOSE cur_employee;
&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Aug 2012 09:46:55 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/execute-statement-in-the-loop/qaa-p/13835882#M4866725</guid>
      <dc:creator>regdomaratzki</dc:creator>
      <dc:date>2012-08-31T09:46:55Z</dc:date>
    </item>
  </channel>
</rss>

