{"id":34767,"date":"2021-06-16T13:57:20","date_gmt":"2021-06-16T13:57:20","guid":{"rendered":"https:\/\/ittutorial.org\/?p=34767"},"modified":"2021-06-16T13:57:20","modified_gmt":"2021-06-16T13:57:20","slug":"utl_file-fopen-in-pl-sql","status":"publish","type":"post","link":"https:\/\/ittutorial.org\/utl_file-fopen-in-pl-sql\/","title":{"rendered":"UTL_FILE FOPEN in PL\/SQL"},"content":{"rendered":"<p>I will explain UTL_FILE FOPEN in PL\/SQL in this post.<!--more--><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #ff0000;\">UTL_FILE in Oracle<\/span><\/h2>\n<p>UTL_FILE package is used to restricted version of operating system stream file I\/O in PL\/SQL Programs that can read and write operating system text files<\/p>\n<p>&nbsp;<\/p>\n\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #ff0000;\">FOPEN function of\u00a0UTL_FILE<\/span><\/h3>\n<p>You can open the operating system file in the database using fopen function, you can edit and manipulate the file content using FOPEN Function.<\/p>\n<p>&nbsp;<\/p>\n<h3 class=\"titleinrefsubsect\"><span style=\"color: #ff0000;\">UTL_FILE.FOPEN Syntax<\/span><\/h3>\n<pre>UTL_FILE.FOPEN (\r\n   location     IN VARCHAR2,\r\n   filename     IN VARCHAR2,\r\n   open_mode    IN VARCHAR2,\r\n   max_linesize IN BINARY_INTEGER) \r\n  RETURN file_type;\r\n\r\n\r\n\r\n<\/pre>\n<table class=\"cellalignment1526\" dir=\"ltr\" title=\"FOPEN Function Parameters\" summary=\"This table describes the Parameters of the UTL_FILE.FOPEN subprogram.\">\n<thead>\n<tr class=\"cellalignment1517\">\n<th id=\"r1c1-t16\" class=\"cellalignment1524\">Parameter<\/th>\n<th id=\"r1c2-t16\" class=\"cellalignment1524\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr class=\"cellalignment1517\">\n<td id=\"r2c1-t16\" class=\"cellalignment1525\" headers=\"r1c1-t16\"><code>location<\/code><\/td>\n<td class=\"cellalignment1525\" headers=\"r2c1-t16 r1c2-t16\">Directory location of file. This string is a directory object name and is case sensitive. The default is uppercase. Read privileges must be granted on this directory object for the\u00a0<code>UTL_FILE<\/code>\u00a0user to run\u00a0<code>FOPEN<\/code>.<\/td>\n<\/tr>\n<tr class=\"cellalignment1517\">\n<td id=\"r3c1-t16\" class=\"cellalignment1525\" headers=\"r1c1-t16\"><code>filename<\/code><\/td>\n<td class=\"cellalignment1525\" headers=\"r3c1-t16 r1c2-t16\">File name, including extension (file type), without directory path. If a directory path is given as a part of the filename, it is ignored by\u00a0<code>FOPEN<\/code>. On Unix, the filename cannot end with \/.<\/td>\n<\/tr>\n<tr class=\"cellalignment1517\">\n<td id=\"r4c1-t16\" class=\"cellalignment1525\" headers=\"r1c1-t16\"><code>open_mode<\/code><\/td>\n<td class=\"cellalignment1525\" headers=\"r4c1-t16 r1c2-t16\">Specifies how the file is opened. Modes include:<\/p>\n<p>r &#8212; read text<\/p>\n<p>w &#8212; write text<\/p>\n<p>a &#8212; append text<\/p>\n<p>rb &#8212; read byte mode<\/p>\n<p>wb &#8212; write byte mode<\/p>\n<p>ab &#8212; append byte mode<\/p>\n<p>If you try to open a file specifying &#8216;<code>a<\/code>&#8216; or &#8216;<code>ab<\/code>&#8216; for\u00a0<code>open_mode<\/code>\u00a0but the file does not exist, the file is created in\u00a0<code>write<\/code>\u00a0mode.<\/td>\n<\/tr>\n<tr class=\"cellalignment1517\">\n<td id=\"r5c1-t16\" class=\"cellalignment1525\" headers=\"r1c1-t16\"><code>max_linesize<\/code><\/td>\n<td class=\"cellalignment1525\" headers=\"r5c1-t16 r1c2-t16\">Maximum number of characters for each line, including the newline character, for this file (minimum value 1, maximum value 32767). If unspecified, Oracle supplies a default value of 1024.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre>\r\n\r\n\r\n\r\n\r\n<\/pre>\n<p>You can use the UTL_FILE and UTL_FILE.FOPEN function in the example as follows.<\/p>\n<p>&nbsp;<\/p>\n<pre>DECLARE \r\nV1 VARCHAR2(32767); \r\nF1 UTL_FILE.FILE_TYPE; \r\nBEGIN \r\n-- In this example MAX_LINESIZE is less than GET_LINE's length request \r\n-- so the number of bytes returned will be 256 or less if a line terminator is seen. \r\nF1 := UTL_FILE.FOPEN('MYDIR','MYFILE','R',256); \r\nUTL_FILE.GET_LINE(F1,V1,32767); \r\nUTL_FILE.FCLOSE(F1);\r\n\r\n-- In this example, FOPEN's MAX_LINESIZE is NULL and defaults to 1024, \r\n-- so the number of bytes returned will be 1024 or less if a line terminator is seen. \r\nF1 := UTL_FILE.FOPEN('MYDIR','MYFILE','R'); \r\nUTL_FILE.GET_LINE(F1,V1,32767); \r\nUTL_FILE.FCLOSE(F1);\r\n\r\n-- In this example, GET_LINE doesn't specify a number of bytes, so it defaults to \r\n-- the same value as FOPEN's MAX_LINESIZE which is NULL in this case and defaults to 1024. \r\n-- So the number of bytes returned will be 1024 or less if a line terminator is seen. \r\nF1 := UTL_FILE.FOPEN('MYDIR','MYFILE','R'); \r\nUTL_FILE.GET_LINE(F1,V1); \r\nUTL_FILE.FCLOSE(F1); \r\nEND;<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h5><a href=\"https:\/\/ittutorial.org\/performance-tuning-and-sql-tuning-tutorial-in-the-oracle-database\/\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color: #ff0000;\">Do you want to learn Oracle Database Performance Tuning detailed, then Click this link.<\/span><\/a><\/h5>\n<blockquote class=\"wp-embedded-content\" data-secret=\"vZHO83tzgm\"><p><a href=\"https:\/\/ittutorial.org\/performance-tuning-and-sql-tuning-tutorial-in-the-oracle-database\/\">Performance Tuning and SQL Tuning Tutorial in the Oracle Database<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Performance Tuning and SQL Tuning Tutorial in the Oracle Database&#8221; &#8212; IT Tutorial\" src=\"https:\/\/ittutorial.org\/performance-tuning-and-sql-tuning-tutorial-in-the-oracle-database\/embed\/#?secret=koXzfLBmrm#?secret=vZHO83tzgm\" data-secret=\"vZHO83tzgm\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I will explain UTL_FILE FOPEN in PL\/SQL in this post.<\/p>\n","protected":false},"author":1,"featured_media":14361,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1994,3647],"tags":[19730,19734,19731,19733,19732,19736,19738,19735,19737],"class_list":["post-34767","post","type-post","status-publish","format-standard","has-post-thumbnail","","category-oracle","category-pl-sql","tag-utl_file-fopen","tag-utl_file-fopen-wildcard","tag-utl_file-fopen-12c","tag-utl_file-fopen-19c","tag-utl_file-fopen-example-oracle-12c","tag-utl_file-fopen-example-oracle-19c","tag-utl_file-fopen-invalid-directory-path","tag-utl_file-fopen-invalid-file-operation","tag-utl_file-fopen-max_linesize"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/04\/plsql.png","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts\/34767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/comments?post=34767"}],"version-history":[{"count":1,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts\/34767\/revisions"}],"predecessor-version":[{"id":34768,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts\/34767\/revisions\/34768"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/media\/14361"}],"wp:attachment":[{"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/media?parent=34767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/categories?post=34767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/tags?post=34767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}