{"id":1863,"date":"2011-04-17T15:10:09","date_gmt":"2011-04-17T09:40:09","guid":{"rendered":"http:\/\/sendyoursmiles.com\/?p=1863"},"modified":"2011-04-17T15:10:09","modified_gmt":"2011-04-17T09:40:09","slug":"utl_file-example","status":"publish","type":"post","link":"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example","title":{"rendered":"UTL_FILE Read Example"},"content":{"rendered":"<p>This script can be used to read data from a flat file and insert into a table.<\/p>\n<p>UTL_FILE Read<\/p>\n<p><code><br \/>\nCREATE OR REPLACE PROCEDURE load_data(errbuff varchar2,errcode number)<br \/>\nAS<br \/>\nv_line VARCHAR2(2000); -- Data line read from input file<br \/>\nv_file UTL_FILE.FILE_TYPE; -- Data file handle<br \/>\nv_dir VARCHAR2(250); -- Directory containing the data file<br \/>\nv_filename VARCHAR2(50); -- Data filename<br \/>\nv_1st_Comma number;<br \/>\nv_2nd_Comma number;<br \/>\nv_3rd_Comma number;<br \/>\nv_4th_Comma number;<br \/>\nv_5th_Comma number;<\/p>\n<p>v_empno sample_emp.empno%type;<br \/>\nv_ename sample_emp.ename%type;<br \/>\nv_job sample_emp.job%type;<br \/>\nv_mgr sample_emp.mgr%type;<br \/>\nv_hiredate sample_emp.hiredate%type;<br \/>\n--v_sal sample_emp.sal%type;<\/p>\n<p>BEGIN<\/p>\n<p>v_dir := '\/usr\/tmp';<br \/>\nv_filename := 'sample.dat';<br \/>\nv_file := UTL_FILE.FOPEN(v_dir, v_filename, 'r');<\/p>\n<p>-- --------------------------------------------------------<br \/>\n-- Loop over the file, reading in each line. GET_LINE will<br \/>\n-- raise NO_DATA_FOUND when it is done, so we use that as<br \/>\n-- the exit condition for the loop.<br \/>\n-- --------------------------------------------------------<\/p>\n<p>LOOP<br \/>\nBEGIN<\/p>\n<p>UTL_FILE.GET_LINE(v_file, v_line);<\/p>\n<p>EXCEPTION<br \/>\nWHEN no_data_found THEN<br \/>\nexit;<br \/>\nEND;<\/p>\n<p>-- ----------------------------------------------------------<br \/>\n-- Each field in the input record is delimited by commas. We<br \/>\n-- need to find the locations of the two commas in the line,<br \/>\n-- and use these locations to get the fields from v_line.<br \/>\n-- ----------------------------------------------------------<\/p>\n<p>v_1st_Comma := INSTR(v_line, ',' ,1 , 1);<br \/>\nv_2nd_Comma := INSTR(v_line, ',' ,1 , 2);<br \/>\nv_3rd_Comma := INSTR(v_line, ',' ,1 , 3);<br \/>\nv_4th_Comma := INSTR(v_line, ',' ,1 , 4);<br \/>\nv_5th_Comma := INSTR(v_line, ',' ,1 , 5);<\/p>\n<p>v_empno := to_number(SUBSTR(v_line, 1, v_1st_Comma-1));<br \/>\nv_ename := SUBSTR(v_line, v_1st_Comma+1, v_2nd_Comma-v_1st_Comma-1);<br \/>\nv_job := SUBSTR(v_line, v_2nd_comma+1, v_3rd_Comma-v_2nd_Comma-1);<br \/>\nv_mgr := to_number(SUBSTR(v_line, v_3rd_comma+1, v_4th_Comma-v_3rd_Comma-1));<br \/>\nv_hiredate := to_date(SUBSTR(v_line, v_4th_comma+1, v_5th_Comma-v_4th_Comma-1),'DD-MON-YYYY');<br \/>\n-- v_sal := to_number(SUBSTR(v_line, v_5th_comma+1),'99999');<\/p>\n<p>DBMS_OUTPUT.PUT_LINE(v_empno ||' '|| v_ename || ' ' || v_job || ' ' || v_mgr ||' ' || v_hiredate);<\/p>\n<p>-- ------------------------------------------<br \/>\n-- Insert the new record into the DEPT table.<br \/>\n-- ------------------------------------------<\/p>\n<p>INSERT INTO sample_emp<br \/>\nVALUES (v_empno,v_ename,v_job,v_mgr,v_hiredate);<br \/>\nEND LOOP;<\/p>\n<p>UTL_FILE.FCLOSE(v_file);<\/p>\n<p>COMMIT;<\/p>\n<p>END;<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This script can be used to read data from a flat file and insert into a table. UTL_FILE Read CREATE OR REPLACE PROCEDURE load_data(errbuff varchar2,errcode number) AS v_line VARCHAR2(2000); &#8212; Data line read from input file v_file UTL_FILE.FILE_TYPE; &#8212; Data file handle v_dir VARCHAR2(250); &#8212; Directory containing the data file v_filename VARCHAR2(50); &#8212; Data filename [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-1863","post","type-post","status-publish","format-standard","hentry","category-scripts"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>UTL_FILE example<\/title>\n<meta name=\"description\" content=\"Read the data from datafile using UTL_FILE\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prudhvi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example#article\",\"isPartOf\":{\"@id\":\"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example\"},\"author\":{\"name\":\"Prudhvi\",\"@id\":\"https:\/\/erpschools.com\/erps\/#\/schema\/person\/dbed9bb7fb66aa7a700fc565da024512\"},\"headline\":\"UTL_FILE Read Example\",\"datePublished\":\"2011-04-17T09:40:09+00:00\",\"dateModified\":\"2011-04-17T09:40:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example\"},\"wordCount\":24,\"commentCount\":10,\"publisher\":{\"@id\":\"https:\/\/erpschools.com\/erps\/#organization\"},\"articleSection\":[\"Scripts\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example\",\"url\":\"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example\",\"name\":\"UTL_FILE example\",\"isPartOf\":{\"@id\":\"https:\/\/erpschools.com\/erps\/#website\"},\"datePublished\":\"2011-04-17T09:40:09+00:00\",\"dateModified\":\"2011-04-17T09:40:09+00:00\",\"description\":\"Read the data from datafile using UTL_FILE\",\"breadcrumb\":{\"@id\":\"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/erpschools.com\/erps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"UTL_FILE Read Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/erpschools.com\/erps\/#website\",\"url\":\"https:\/\/erpschools.com\/erps\/\",\"name\":\"erpSchools\",\"description\":\"Oracle Apps\",\"publisher\":{\"@id\":\"https:\/\/erpschools.com\/erps\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/erpschools.com\/erps\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/erpschools.com\/erps\/#organization\",\"name\":\"erpSchools\",\"url\":\"https:\/\/erpschools.com\/erps\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/erpschools.com\/erps\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/erpschools.com\/erps\/wp-content\/uploads\/img\/erps_logo7.png\",\"contentUrl\":\"https:\/\/erpschools.com\/erps\/wp-content\/uploads\/img\/erps_logo7.png\",\"width\":250,\"height\":60,\"caption\":\"erpSchools\"},\"image\":{\"@id\":\"https:\/\/erpschools.com\/erps\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"http:\/\/facebook.com\/erpschools\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/erpschools.com\/erps\/#\/schema\/person\/dbed9bb7fb66aa7a700fc565da024512\",\"name\":\"Prudhvi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/erpschools.com\/erps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/488cec3605845b95cb20e60c67a8f5c7e74b65a305525c8006315d524f120db9?s=96&d=blank&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/488cec3605845b95cb20e60c67a8f5c7e74b65a305525c8006315d524f120db9?s=96&d=blank&r=g\",\"caption\":\"Prudhvi\"},\"sameAs\":[\"http:\/\/www.erpschools.com\"],\"url\":\"https:\/\/erpschools.com\/erps\/author\/prudhvi\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"UTL_FILE example","description":"Read the data from datafile using UTL_FILE","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example","twitter_misc":{"Written by":"Prudhvi","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example#article","isPartOf":{"@id":"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example"},"author":{"name":"Prudhvi","@id":"https:\/\/erpschools.com\/erps\/#\/schema\/person\/dbed9bb7fb66aa7a700fc565da024512"},"headline":"UTL_FILE Read Example","datePublished":"2011-04-17T09:40:09+00:00","dateModified":"2011-04-17T09:40:09+00:00","mainEntityOfPage":{"@id":"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example"},"wordCount":24,"commentCount":10,"publisher":{"@id":"https:\/\/erpschools.com\/erps\/#organization"},"articleSection":["Scripts"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/erpschools.com\/erps\/scripts\/utl_file-example#respond"]}]},{"@type":"WebPage","@id":"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example","url":"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example","name":"UTL_FILE example","isPartOf":{"@id":"https:\/\/erpschools.com\/erps\/#website"},"datePublished":"2011-04-17T09:40:09+00:00","dateModified":"2011-04-17T09:40:09+00:00","description":"Read the data from datafile using UTL_FILE","breadcrumb":{"@id":"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/erpschools.com\/erps\/scripts\/utl_file-example"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/erpschools.com\/erps\/scripts\/utl_file-example#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/erpschools.com\/erps"},{"@type":"ListItem","position":2,"name":"UTL_FILE Read Example"}]},{"@type":"WebSite","@id":"https:\/\/erpschools.com\/erps\/#website","url":"https:\/\/erpschools.com\/erps\/","name":"erpSchools","description":"Oracle Apps","publisher":{"@id":"https:\/\/erpschools.com\/erps\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/erpschools.com\/erps\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/erpschools.com\/erps\/#organization","name":"erpSchools","url":"https:\/\/erpschools.com\/erps\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/erpschools.com\/erps\/#\/schema\/logo\/image\/","url":"https:\/\/erpschools.com\/erps\/wp-content\/uploads\/img\/erps_logo7.png","contentUrl":"https:\/\/erpschools.com\/erps\/wp-content\/uploads\/img\/erps_logo7.png","width":250,"height":60,"caption":"erpSchools"},"image":{"@id":"https:\/\/erpschools.com\/erps\/#\/schema\/logo\/image\/"},"sameAs":["http:\/\/facebook.com\/erpschools"]},{"@type":"Person","@id":"https:\/\/erpschools.com\/erps\/#\/schema\/person\/dbed9bb7fb66aa7a700fc565da024512","name":"Prudhvi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/erpschools.com\/erps\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/488cec3605845b95cb20e60c67a8f5c7e74b65a305525c8006315d524f120db9?s=96&d=blank&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/488cec3605845b95cb20e60c67a8f5c7e74b65a305525c8006315d524f120db9?s=96&d=blank&r=g","caption":"Prudhvi"},"sameAs":["http:\/\/www.erpschools.com"],"url":"https:\/\/erpschools.com\/erps\/author\/prudhvi"}]}},"_links":{"self":[{"href":"https:\/\/erpschools.com\/erps\/wp-json\/wp\/v2\/posts\/1863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/erpschools.com\/erps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/erpschools.com\/erps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/erpschools.com\/erps\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/erpschools.com\/erps\/wp-json\/wp\/v2\/comments?post=1863"}],"version-history":[{"count":0,"href":"https:\/\/erpschools.com\/erps\/wp-json\/wp\/v2\/posts\/1863\/revisions"}],"wp:attachment":[{"href":"https:\/\/erpschools.com\/erps\/wp-json\/wp\/v2\/media?parent=1863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/erpschools.com\/erps\/wp-json\/wp\/v2\/categories?post=1863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/erpschools.com\/erps\/wp-json\/wp\/v2\/tags?post=1863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}