{"id":1775,"date":"2017-12-21T15:07:15","date_gmt":"2017-12-21T14:07:15","guid":{"rendered":"http:\/\/62.131.51.129\/?p=1775"},"modified":"2017-12-21T15:07:15","modified_gmt":"2017-12-21T14:07:15","slug":"just-some-updates-on-plsql","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=1775","title":{"rendered":"Just some updates on PLSQL"},"content":{"rendered":"<p>From time to time, I just repeat old exercises. I do this to maintain my knowledge before it gets lost in the mist of time. One such areas is PLSQL. You never forget that PL SQL is used to maintain data on an Oracle database. But I do forget tiny details such as how to insert a parameter.<br \/>I like to insert the PLSQL code in the client tool SQL Developper. This tool clearly shows where the code is created. It also shows how the PLSQL functions and procedures are related to other database objects like schemes, tables and views.<\/p>\n<p>Let me show the example. I created one procedure that calls a function. The function looks like:<\/p>\n<pre>create or replace PROCEDURE MAIN AS \n  terug int;\nBEGIN\n  dbms_output.put_line(' Hello World');\n  terug := test(110221);\nEND MAIN;\n<\/pre>\n<p>This procedure is quite simple. It writes something to screen (&#8216;Hello World&#8217;) and it calls a function with a parameter (110221). It can be invoked from the command line by the command &#8220;exec main&#8221;. in this case &#8220;main&#8221; is the name of the procedure and &#8220;exec&#8221; indicates that a procedure must be run.<br \/>Starting this from sqlplus looks like:<br \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1781\" src=\"http:\/\/62.131.51.129\/wp-content\/uploads\/2017\/12_01\/Untitled.png\" alt=\"\" width=\"788\" height=\"287\" \/><\/p>\n<p>We see that \u2018Hello World\u2019 is returned.<br \/>The procedure calls a function. This function looks like:<\/p>\n\n\n<pre class=\"wp-block-preformatted\">create or replace function TEST<br> (<br>   INVOER IN INT<br> ) RETURN INT AS<br> v_name            HR.CUSTOMER_IND.CURPNM%TYPE;<br> n_table           INT;<br> teller            INT:=0;<br> l_uitvoer         VARCHAR(100);<br> p_table_name      VARCHAR(100);<br> stmt              VARCHAR(250);<br> naam              VARCHAR(100);<br> CURSOR cur_chief<br> IS<br>   select curptw into v_name<br>   from CUSTOMER_IND where CUNOCU=invoer;<br> r_chief cur_chief%ROWTYPE;<br> BEGIN<br>   p_table_name :='ff';<br>   SELECT COUNT(*) INTO n_table FROM user_tables WHERE table_name = UPPER(p_table_name);<br>   IF (n_table = 0) THEN<br>     EXECUTE IMMEDIATE 'create table ' || p_table_name || '(teller int, naam varchar(50))';<br>   else<br>     EXECUTE IMMEDIATE 'truncate table ' || p_table_name;<br>   END IF;<br>   OPEN cur_chief;<br>   LOOP<br>      teller := teller + 1;<br>      FETCH cur_chief INTO r_chief;<br>      EXIT WHEN cur_chief%NOTFOUND;<br>      naam := r_chief.CURPTW;<br>      stmt := 'insert into '||p_table_name || ' (teller, naam) values(' || teller ||','''||naam||''')';<br>      DBMS_OUTPUT.PUT_LINE(stmt);<br>      EXECUTE IMMEDIATE stmt;<br>   END LOOP;<br>   RETURN 1;<br> END TEST;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> This function contains some ideas that I like. The first idea is the creation of a cursor that contains an outcome set. This cursor is based on a table for which the code is given <a href=\"http:\/\/www.van-maanen.com\/wp-content\\uploads\\2017\\12_01\/export.sql\">here<\/a>. This outcome is made dependent on a parameter. The set of outcomes will be translated into a series of records that will be translated one by one. Such a record is indicated by variable &#8220;r_chief&#8221;.<br>Another trick is a check whether a certain table already exists. This is done by checking the user_tables. If it is already present, the count will be larger than 0. In that case a command will be executed that truncates the table.<br>A final trick is looping through the cursor. Each loop writes one record to &#8220;r_chief&#8221;. From that record, some values are retrieved. The values are then stored in an insert statement. The most tricky part is getting an apostrophe around the value of a string. The apostrophe is also used to terminate a string in the PLSQL code. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>From time to time, I just repeat old exercises. I do this to maintain my knowledge before it gets lost in the mist of time. One such areas is PLSQL. You never forget that PL SQL is used to maintain data on an Oracle database. But I do forget tiny details such as how to [&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-1775","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\/1775","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=1775"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/1775\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1775"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}