{"id":2996,"date":"2019-03-23T22:54:55","date_gmt":"2019-03-23T21:54:55","guid":{"rendered":"http:\/\/van-maanen.com\/?p=2996"},"modified":"2019-03-23T22:54:55","modified_gmt":"2019-03-23T21:54:55","slug":"sql-in-steps","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=2996","title":{"rendered":"SQL in steps"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Let us assume we have a request to show results from a query that is too massive to run on-line. Let us also assume that we may distinguish in this query a series on countries \/ persons that must be run.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In pseudo code, we have the situation that we may have to run: select * from complicated that could be translated into something like: for employees select * from lesscomplicated(employee). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This can be solved in a PLSQL programme that contains a LOOP. Two possible solutions are given below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">create or replace PROCEDURE FORLOOP AS<br>    CURSOR c1 IS SELECT * FROM HR.EMPLOYEES;<br>    i NUMBER:= 0;<br>    v_totalName varchar2(80);<br> BEGIN<br>   FOR e_rec IN c1 LOOP<br>     i:= i+1;<br>     select DEPARTMENTS.department_name into v_totalName from HR.DEPARTMENTS where e_rec.department_id = DEPARTMENTS.department_id;<br>     dbms_output.put_line(i||chr(9)||e_rec.EMPLOYEE_ID||chr(9)||e_rec.LAST_NAME||chr(9)|| v_totalName);<br>   END LOOP;<br> END;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The idea is that we have a series of employees. For each of these employees, we have a simple query for one employee. This simple query for one employee runs fast. The loop guarantees that all employees are considered and all results are returned. A<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An alternative programme is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">create or replace PROCEDURE FORLOOP AS<br>    CURSOR c1 IS SELECT * FROM HR.EMPLOYEES;<br>    e_rec c1%ROWTYPE;<br>    i NUMBER:= 0;<br>    v_totalName varchar2(80);<br> BEGIN<br>   OPEN c1;<br>   i:=0;<br>   LOOP<br>     FETCH c1 into e_rec;<br>     EXIT WHEN c1%NOTFOUND;<br>     i:= i+1;<br>     select DEPARTMENTS.department_name into v_totalName from HR.DEPARTMENTS where e_rec.department_id = DEPARTMENTS.department_id;<br>     dbms_output.put_line(i||chr(9)||e_rec.EMPLOYEE_ID||chr(9)||e_rec.LAST_NAME||chr(9)|| v_totalName);<br>   END LOOP;<br> END;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Let us assume we have a request to show results from a query that is too massive to run on-line. Let us also assume that we may distinguish in this query a series on countries \/ persons that must be run. In pseudo code, we have the situation that we may have to run: select [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2996","post","type-post","status-publish","format-standard","hentry","category-nice-to-know"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/2996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2996"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/2996\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2996"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}