Utl_file raise INVALID_WRITE_ERROR

Hi all,
I try to use utl_file package provided by Oracle 8.1.5 to write a text file in XML format. Oracle runs in UNIX environment.
I use the following command to open a new file:
UTL_FILE.FOPEN(P_PATH,P_FILENAME,'w',32767);
I use the put_line to write a line, such as
UTL_FILE.PUT_LINE(P_FILEHANDLE,' <Font x:Family="Swiss" ss:Bold="1"/>');
The problem is that utl_file always raises UTL_FILE.WRITE_ERROR after writting 234 lines and 8192 characters . Is there potential limition set by Oracle or Unix for the size of the written file?
Thanks

Hi,
Sorry, I don't have access to Oracle 8 documentation. The following is based on Oracle 11, but I'd be surprised if it was different for earlier versions.
There's no limit to the size of the file.
There is a limit to the size of a line, which you're already setting by passing the fourth argument (32767) to fopen. That means you can't write more than 32767 characters in a single call to put_line, or in consecutive calls to put.
I don't see any obvious mistakes in what you posted.
Post a complete program that gets the same error.

Similar Messages

  • Generating Excel file using PL/SQL

    Hi,
    I wanted to generate the excel file using the below pasted PL/SQL which I have downloaded from one of the tech sites. And as I have very limited knowledge about PL/SQL I really dont know how & where I should compile this below mentioned code and get the desired O/P?
    Please reply me ASAP if anybody can help me with this?
    Please see the below code and please help me to interpret the same.
    CREATE OR REPLACE PACKAGE gen_xl_xml IS
    -- Version 0.5
    -- Objective : The main objective OF this PACKAGE IS TO CREATE excel files from PL/SQL code
    -- Requirement : Excel version 2003 onwards support XML format.
    -- The procedures does have TO be called IN specific order at this moment.
    -- expected SEQUENCE AS OF now IS
    -- At first call create_file -> It creates a FILE WITH NAME that you pass AS parameter. This PROCEDURE writes the
    -- excel file header AND some basic requirments like default style.
    -- procedures 1. create_style , create_worksheet AND write_cell can be used IN ANY SEQUENCE AS many
    -- times AS you need.
    -- When done WITH writing TO FILE call the PROCEDURE close_file
    -- CLOSE FILE --> This will actually flush the data INTO the worksheet(s) one BY one and then close the file.
    -- What colors I can use ?
    -- red , blue , green, gray , YELLOW, BROWN , PINK . lightgray ,
    debug_flag BOOLEAN := TRUE ;
    PROCEDURE create_excel( p_directory IN VARCHAR2 DEFAULT NULL , p_file_name IN VARCHAR2 DEFAULT NULL ) ;
    PROCEDURE create_excel_apps ;
    PROCEDURE create_style( p_style_name IN VARCHAR2
    , p_fontname IN VARCHAR2 DEFAULT NULL
    , p_fontcolor IN VARCHAR2 DEFAULT 'Black'
    , p_fontsize IN NUMBER DEFAULT null
    , p_bold IN BOOLEAN DEFAULT FALSE
    , p_italic IN BOOLEAN DEFAULT FALSE
    , p_underline IN VARCHAR2 DEFAULT NULL
    , p_backcolor IN VARCHAR2 DEFAULT NULL );
    PROCEDURE close_file ;
    PROCEDURE create_worksheet( p_worksheet_name IN VARCHAR2 ) ;
    PROCEDURE write_cell_num(p_row NUMBER , p_column NUMBER, p_worksheet_name IN VARCHAR2, p_value IN NUMBER , p_style IN VARCHAR2 DEFAULT NULL );
    PROCEDURE write_cell_char(p_row NUMBER, p_column NUMBER, p_worksheet_name IN VARCHAR2, p_value IN VARCHAR2, p_style IN VARCHAR2 DEFAULT NULL );
    PROCEDURE write_cell_null(p_row NUMBER , p_column NUMBER , p_worksheet_name IN VARCHAR2, p_style IN VARCHAR2 );
    PROCEDURE set_row_height( p_row IN NUMBER , p_height IN NUMBER, p_worksheet IN VARCHAR2 ) ;
    PROCEDURE set_column_width( p_column IN NUMBER , p_width IN NUMBER , p_worksheet IN VARCHAR2 ) ;
    END ;
    -- Package : gen_xl_sml
    -- Version : 0.62
    CREATE OR REPLACE PACKAGE Body gen_xl_xml IS
    -- worksheets must be created before it could be passed AS parameter TO the write cell procedures
    l_file utl_FILE.file_type ;
    g_apps_env VARCHAR2(1) := 'U' ; -- unset at the start
    TYPE tbl_excel_data IS TABLE OF VARCHAR2(2000) INDEX BY BINARY_INTEGER ;
    g_excel_data tbl_excel_data ;
    g_null_data tbl_excel_data ;
    g_data_count NUMBER ;
    TYPE rec_styles IS record ( s VARCHAR2(30) , def VARCHAR2(2000) );
    TYPE tbl_style IS TABLE OF rec_styles INDEX BY BINARY_INTEGER ;
    g_styles tbl_style ;
    g_null_styles tbl_style ;
    g_style_count NUMBER := 0;
    TYPE rec_worksheets IS record ( w VARCHAR2(30) , whdr VARCHAR2(300), wftr VARCHAR2(2000) );
    TYPE tbl_worksheets IS TABLE OF rec_worksheets INDEX BY BINARY_INTEGER ;
    g_worksheets tbl_worksheets ;
    g_null_worksheets tbl_worksheets ;
    g_worksheets_count NUMBER := 0;
    TYPE rec_cell_data IS record ( r NUMBER , c NUMBER , v VARCHAR2(2000) ,s VARCHAR2(30) , w VARCHAR2(100), dt VARCHAR2(8) );
    TYPE tbl_cell_data IS TABLE OF rec_cell_data INDEX BY binary_INTEGER ;
    g_cells tbl_cell_data ;
    g_null_cells tbl_cell_data ;
    g_cell_count NUMBER := 0 ;
    TYPE rec_columns_data IS record( c NUMBER, wd NUMBER, w VARCHAR2(30) ) ;
    TYPE tbl_columns_data IS TABLE OF rec_columns_data Index BY BINARY_INTEGER ;
    g_columns tbl_columns_data ;
    g_null_columns tbl_columns_data ;
    g_column_count NUMBER ;
    TYPE rec_rows_data IS record( r NUMBER, ht NUMBER , w VARCHAR2(30) ) ;
    TYPE tbl_rows_data IS TABLE OF rec_rows_data Index BY BINARY_INTEGER ;
    g_rows tbl_ROWS_data ;
    g_null_rows tbl_rows_data ;
    g_ROW_count NUMBER ;
    PROCEDURE p ( p_string IN VARCHAR2) is
    begin
    IF debug_flag = TRUE THEN
    DBMS_OUTPUT.put_line( p_string) ;
    END IF;
    END ;
    FUNCTION style_defined ( p_style IN VARCHAR2 ) RETURN BOOLEAN IS
    BEGIN
    FOR i IN 1..g_style_count LOOP
    IF g_styles(i).s = p_style THEN
    RETURN TRUE ;
    END IF;
    END LOOP ;
    RETURN FALSE ;
    END ;
    -- Function : cell_used returns : BOOLEAN
    -- Description : Cell_used FUNCTION returns TRUE IF that cell IS already used
    -- Called BY : write_Cell_char, write_cell_num
    -- ??? right now it IS NOT called BY write_Cell_null , this needs TO be evaluated
    FUNCTION cell_used ( p_r IN NUMBER , p_c IN number , p_w IN VARCHAR2 ) RETURN BOOLEAN IS
    BEGIN
    FOR i IN 1..g_cell_count LOOP
    IF ( g_cells(i).r = p_r AND g_cells(i).c = p_c AND g_cells(i).w = p_w ) THEN
    RETURN TRUE ;
    END IF;
    END LOOP ;
    RETURN FALSE ;
    END ;
    PROCEDURE initialize_collections IS
    --- following lines resets the cell data and the cell count as it was
    -- observed that the data is retained across the two runs within same seseion.
    BEGIN
    g_cells := g_null_cells ;
    g_Cell_count := 0 ;
    g_styles := g_null_styles ;
    g_style_count := 0 ;
    g_rows := g_null_rows ;
    g_ROW_count := 0 ;
    g_columns := g_null_columns ;
    g_column_count := 0 ;
    g_excel_data := g_null_data ;
    g_data_count := 0 ;
    g_apps_env := 'U';
    g_worksheets := g_null_worksheets ;
    g_worksheets_count := 0;
    END ;
    PROCEDURE create_excel_apps is
    BEGIN
    -- CHECK the env value
    IF g_apps_env = 'N' THEN
    raise_application_error( -20001 , 'You have already called create_excel ( Non Apps ) procedure, Can not set env to create_excel_apps.');
    END IF ;
    initialize_collections ;
    g_apps_env := 'Y' ;
    END ;
    PROCEDURE create_excel( p_directory IN VARCHAR2 DEFAULT NULL , p_file_name IN VARCHAR2 DEFAULT NULL )
    IS
    BEGIN
    -- CHECK the env value
    IF g_apps_env = 'Y' THEN
    raise_application_error( -20001 , 'You have already called procedure create_excel_apps , Can not set env to Non-Apps create_excel.');
    END IF ;
    initialize_collections ;
    g_apps_env := 'N' ;
    IF ( p_directory IS NULL OR p_file_name IS NULL ) THEN
    raise_application_error( -20001 , 'p_directory and p_file_name must be not null');
    END IF ;
    BEGIN
    -- Open the FILE IN the specified directory
    l_file := utl_file.fopen( p_directory, p_file_name , 'w') ;
    EXCEPTION
    WHEN utl_file.write_error THEN
    raise_application_error( -20101 , 'UTL_FILE raised write error, check if file is already open or directory access');
    WHEN utl_file.INVALID_OPERATION THEN
    raise_application_error( -20101 , 'UTL_FILE could not open file or operate on it, check if file is already open.');
    WHEN utl_file.invalid_path THEN
    raise_application_error( -20101 , 'UTL_FILE raised invalid path, check the directory passed is correct and you have access to it.');
    WHEN others THEN
    raise_application_error( -20101 , 'UTL_FILE raised others exception ');
    END ;
    p( 'File '||p_file_name ||' created successfully');
    END ;
    PROCEDURE create_style( p_style_name IN VARCHAR2
    , p_fontname IN VARCHAR2 DEFAULT NULL
    , p_fontcolor IN VARCHAR2 DEFAULT 'Black'
    , p_fontsize IN NUMBER DEFAULT null
    , p_bold IN BOOLEAN DEFAULT FALSE
    , p_italic IN BOOLEAN DEFAULT FALSE
    , p_underline IN VARCHAR2 DEFAULT NULL
    , p_backcolor IN VARCHAR2 DEFAULT NULL ) is
    l_style VARCHAR2(2000) ;
    l_font VARCHAR2(1200);
    BEGIN
    --- CHECK IF this style IS already defined AND RAISE ERROR IF yes
    IF style_defined( p_style_name ) THEN
    RAISE_application_error( -20001 , 'Style "'||p_style_name ||'" is already defined.');
    END IF;
    g_style_count := g_style_count + 1;
    ---- ??? pass ANY value OF underline AND it will only use single underlines
    -- ??? pattern IS NOT handleed
    IF upper(p_style_name) = 'DEFAULT' THEN
    RAISE_application_error( -20001 , 'Style name DEFAULT is not allowed ');
    END IF ;
    IF upper(p_style_name) IS NULL THEN
    RAISE_application_error( -20001 , 'Style name can not be null ');
    END IF ;
    g_styles(g_style_count).s := p_style_name ;
    g_styles(g_style_count).def := ' <Style ss:ID="'|| p_style_name ||'"> ' ;
    l_font := ' <Font ' ;
    IF p_fontname IS NOT NULL THEN
    l_font :=l_font || 'ss:FontName="'|| p_fontname ||'" ';
    end if ;
    IF p_fontsize is not null then
    l_font := l_font ||' ss:Size="'|| p_fontsize ||'" ';
    end if ;
    IF p_fontcolor is not null then
    l_font := l_font ||' ss:Color="'|| p_fontcolor ||'" ';
    ELSE
    l_font := l_font ||' ss:Color="Black" ';
    end if ;
    IF p_bold = TRUE THEN
    l_font := l_font ||' ss:Bold="1" ' ;
    END IF;
    IF p_italic = TRUE THEN
    l_font := l_font ||' ss:Italic="1" ' ;
    END IF;
    IF p_underline IS NOT NULL THEN
    l_font := l_font ||' ss:Underline="Single" ' ;
    END IF ;
    -- p( l_font );
    g_styles(g_style_count).def := g_styles(g_style_count).def || l_font || '/>' ;
    IF p_backcolor IS NOT NULL THEN
    g_styles(g_style_count).def := g_styles(g_style_count).def || ' <Interior ss:Color="'||p_backcolor ||'" ss:Pattern="Solid"/>' ;
    ELSE
    g_styles(g_style_count).def := g_styles(g_style_count).def || ' <Interior/>';
    END IF ;
    g_styles(g_style_count).def := g_styles(g_style_count).def || ' </Style>' ;
    --- ??? IN font there IS SOME family which IS NOT considered
    END ;
    PROCEDURE close_file IS
    l_last_row NUMBER := 0 ;
    l_dt CHAR ; -- ??? Variable TO store the datatype ; this IS NOT used at this time but may be needed IF the memory
    -- issue IS there FOR example IF there IS big array
    l_style VARCHAR2(140) ;
    l_row_change VARCHAR2(100) ;
    l_file_header VARCHAR2(2000) := '<?xml version="1.0"?>
    <?mso-application progid="Excel.Sheet"?>
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:html="http://www.w3.org/TR/REC-html40">
    <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
    <LastAuthor>a</LastAuthor>
    <Created>1996-10-14T23:33:28Z</Created>
    <LastSaved>2007-05-10T04:00:57Z</LastSaved>
    <Version>11.5606</Version>
    </DocumentProperties>
    <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
    <WindowHeight>9300</WindowHeight>
    <WindowWidth>15135</WindowWidth>
    <WindowTopX>120</WindowTopX>
    <WindowTopY>120</WindowTopY>
    <AcceptLabelsInFormulas/>
    <ProtectStructure>False</ProtectStructure>
    <ProtectWindows>False</ProtectWindows>
    </ExcelWorkbook>
    <Styles>
    <Style ss:ID="Default" ss:Name="Normal">
    <Alignment ss:Vertical="Bottom"/>
    <Borders/>
    <Font/>
    <Interior/>
    <NumberFormat/>
    <Protection/>
    </Style>'
    BEGIN
    IF gen_xl_xml.g_Cell_count = 0 THEN
    raise_application_error( -20007 , 'No cells have been written, this version of gen_xl_xml needs at least one cell to be written');
    END IF;
    IF gen_xl_xml.g_worksheets_count = 0 THEN
    raise_application_error( -20008 , 'No worksheets have been created, this version does not support automatic worksheet creation');
    END IF;
    p( gen_xl_xml.g_Cell_count) ;
    -- Write the header xml part IN the FILE.
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := l_file_header ;
    p( 'Headers written');
    FOR i IN 1..g_style_count LOOP
    p( ' writing style number : '||i);
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := g_styles(i).def ;
    END LOOP ;
    -- CLOSE the styles tag
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := ' </Styles>' ;
    p( 'worksheet count '|| g_worksheets_count );
    FOR j IN 1..g_worksheets_count LOOP
    l_last_row := 0 ; --- FOR every worksheet we need TO CREATE START OF the row
    p( '()()------------------------------------------------------------ last row '||l_last_row );
    --- write the header first
    -- write the COLUMN widhts first
    -- write the cells
    -- write the worksheet footer
    l_row_change := NULL ;
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := ' <Worksheet ss:Name="'|| g_worksheets( j).w ||'"> ' ;
    p( '-------------------------------------------------------------');
    p( '****************.Generated sheet '|| g_worksheets( j).w);
    p( '-------------------------------------------------------------');
    -- write the TABLE structure ??? change the LINE here TO include tha maxrow AND cell
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := '<Table ss:ExpandedColumnCount="16" ss:ExpandedRowCount="44315" x:FullColumns="1" x:FullRows="1">' ;
    FOR i IN 1..g_column_count LOOP
    IF g_columns(i).w = g_worksheets( j).w THEN
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := ' <Column ss:Index="'||g_columns(i).c||'" ss:AutoFitWidth="0" ss:Width="'||g_columns(i).wd ||'"/> ' ;
    END IF;
    END LOOP ;
    -- write the cells data
    FOR i IN 1..g_cell_count LOOP ------ LOOP OF g_cell_count
    p( '()()()()()()()()()()()() '|| i);
    --- we will write only IF the cells belongs TO the worksheet that we are writing.
    IF g_cells(i).w <> g_worksheets(j).w THEN
    p( '........................Cell : W :'|| g_worksheets( j).w ||'=> r='|| g_cells(i).r ||',c ='|| g_cells(i).c||',w='|| g_cells(i).w );
    p( '...Not in this worksheet ');
    -- l_last_row := l_last_row -1 ;
    ELSE
    p( '........................Cell : W :'|| g_worksheets( j).w ||'=> r='|| g_cells(i).r ||',c ='|| g_cells(i).c||',w='|| g_cells(i).w );
    IF g_cells(i).s IS NOT NULL AND NOT style_defined( g_cells(i).s ) THEN
    -- p(g_cells(i).s) ;
    raise_application_error( -20001 , 'Style "'||g_cells(i).s ||'" is not defined, Note : Styles are case sensative and check spaces used while passing style');
    END IF;
    p( '()()------------------------------------------------------------ last row '||l_last_row );
    IF l_last_row = 0 THEN
    FOR t IN 1..g_row_count LOOP
    p( '...Height check => Row =' ||g_rows(t).r ||', w='||g_rows(t).w);
    IF g_rows(t).r = g_cells(i).r AND g_rows(t).w = g_worksheets(j).w THEN
    p( '...Changing height') ;
    l_row_change := ' ss:AutoFitHeight="0" ss:Height="'|| g_rows(t).ht||'" ' ;
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := ' <Row ss:Index="'||g_cells(i).r||'"'|| l_row_change ||'>' ;
    l_last_row := g_cells(i).r ;
    EXIT ;
    ELSE
    p( '...NO change height') ;
    l_row_change := NULL ;
    END IF ;
    END loop ;
    IF l_ROW_CHANGE IS NULL THEN
    g_data_count := g_data_count + 1 ;
    p( '...Creating new row ');
    g_excel_data( g_data_count ) := ' <Row ss:Index="'||g_cells(i).r||'"'|| l_row_change ||'>' ;
    l_last_row := g_cells(i).r ;
    END IF;
    END IF;
    IF g_cells(i).s IS NOT NULL THEN
    p( '...Adding style ');
    l_style := ' ss:StyleID="'||g_cells(i).s||'"' ;
    ELSE
    p( '...No style for this cell ');
    l_style := NULL ;
    END IF;
    p( '()()------------------------------------------------------------ last row '||l_last_row );
    IF g_cells(i).r <> l_last_row THEN
    p('...closing the row.'||g_cells(i).r);
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := ' </Row>' ;
    p( 'ROWCOUNT : '||g_row_count );
    FOR t IN 1..g_ROW_count LOOP
    p( '.....Height check => Row =' ||g_rows(t).r ||', w='||g_rows(t).w);
    IF g_rows(t).r = g_cells(i).r AND g_rows(t).w = g_worksheets(j).w THEN
    p( '.....Changing height') ;
    l_row_change := ' ss:AutoFitHeight="0" ss:Height="'|| g_rows(t).ht||'" ' ;
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := ' <Row ss:Index="'||g_cells(i).r||'"'|| l_row_change ||'>' ;
    EXIT ;
    ELSE
    p( '.....NO change height') ;
    l_row_change := NULL ;
    END IF ;
    END loop ;
    -- P( 'Row :'||g_cells(i).r ||'->'|| l_ROW_CHANGE);
    IF l_row_change IS NULL THEN
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := ' <Row ss:Index="'||g_cells(i).r||'"'|| l_row_change ||'>' ;
    END IF;
    IF g_cells(i).v IS NULL THEN
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := '<Cell ss:Index="'||g_cells(i).c||'"' || l_style ||' ></Cell>';
    ELSE
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := '<Cell ss:Index="'||g_cells(i).c||'"' || l_style ||' ><Data ss:Type="'||g_cells(i).dt ||'">'||g_cells(i).v||'</Data></Cell>';
    END IF ;
    l_last_row :=g_cells(i).r ;
    ELSE
    IF g_cells(i).v IS NULL THEN
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := '<Cell ss:Index="'||g_cells(i).c||'"' || l_style ||' > </Cell>';
    ELSE
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := '<Cell ss:Index="'||g_cells(i).c||'"' || l_style ||' ><Data ss:Type="'||g_cells(i).dt ||'">'||g_cells(i).v||'</Data></Cell>';
    END IF ;
    END IF ;
    END IF ;
    NULL ;
    END LOOP ; -- LOOP OF g_cells_count
    p('...closing the row.');
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := ' </Row>' ;
    -- ??? does following COMMENT will have sheet NAME FOR debugging
    p( '-------------------------------------------------------------');
    p( '....End of writing cell data, closing table tag');
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := ' </Table>' ;
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := g_worksheets(j).wftr ;
    p( '..Closed the worksheet '|| g_worksheets( j).w );
    END LOOP ;
    g_data_count := g_data_count + 1 ;
    g_excel_data( g_data_count ) := '</Workbook>' ;
    p( 'Closed the workbook tag');
    IF g_apps_env = 'N' THEN
    FOR i IN 1..g_data_count LOOP
    utl_FILE.put_line( l_file, g_excel_data(i ));
    END LOOP ;
    utl_file.fclose( l_file );
    p( 'File closed ');
    ELSIF g_apps_env = 'Y' THEN
    FOR i IN 1..g_data_count LOOP
    fnd_file.put_line( fnd_file.output , g_excel_data(i));
    fnd_file.put_line( fnd_file.log , g_excel_data(i));
    END LOOP ;
    ELSE
    raise_application_error( -20001 , 'Env not set, ( Apps or not Apps ) Contact Support.' );
    END IF;
    END ;
    PROCEDURE create_worksheet ( p_worksheet_name IN VARCHAR2 ) IS
    BEGIN
    g_worksheets_count := g_worksheets_count + 1 ;
    g_worksheets(g_worksheets_count).w := p_worksheet_name ;
    g_worksheets(g_worksheets_count).whdr := '<Worksheet ss:Name=" ' || p_worksheet_name ||' ">' ;
    g_worksheets(g_worksheets_count).wftr := '<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
    <ProtectObjects>False</ProtectObjects>
    <ProtectScenarios>False</ProtectScenarios>
    </WorksheetOptions>
    </Worksheet>' ;
    END ;
    PROCEDURE write_cell_char(p_row NUMBER, p_column NUMBER, p_worksheet_name IN VARCHAR2, p_value IN VARCHAR2, p_style IN VARCHAR2 DEFAULT NULL ) IS
    l_ws_exist BOOLEAN ;
    l_worksheet VARCHAR2(2000) ;
    BEGIN
    -- CHECK IF this cell has been used previously.
    IF cell_used( p_row , p_column , p_worksheet_name ) THEN
    RAISE_application_error( -20001 , 'The cell ( Row: '||p_row ||' Column:'||p_column ||' Worksheet:'||p_worksheet_name ||') is already used.Check if you have missed to increment row number in your code. ');
    END IF;
    -- IF worksheet NAME IS NOT passed THEN use first USER created sheet ELSE use DEFAULT sheet
    -- this PROCEDURE just adds the data INTO the g_cells TABLE
    g_cell_count := g_cell_count + 1 ;
    g_cells( g_cell_count ).r := p_row ;
    g_cells( g_cell_count ).c := p_column ;
    g_cells( g_cell_count ).v := p_value ;
    g_cells( g_cell_count ).w := p_worksheet_name ;
    g_cells( g_cell_count ).s := p_style ;
    g_cells( g_cell_count ).dt := 'String' ;
    END ;
    PROCEDURE write_cell_num(p_row NUMBER , p_column NUMBER, p_worksheet_name IN VARCHAR2, p_value IN NUMBER , p_style IN VARCHAR2 DEFAULT NULL ) IS
    l_ws_exist BOOLEAN ;
    l_worksheet VARCHAR2(2000) ;
    BEGIN
    -- ??? IF worksheet NAME IS NOT passed THEN use first USER created sheet ELSE use DEFAULT sheet
    -- this PROCEDURE just adds the data INTO the g_cells TABLE
    -- CHECK IF this cell has been used previously.
    IF cell_used( p_row , p_column , p_worksheet_name ) THEN
    RAISE_application_error( -20001 , 'The cell ( Row: '||p_row ||' Column:'||p_column ||' Worksheet:'||p_worksheet_name ||') is already used. Check if you have missed to increment row number in your code.');
    END IF;
    g_cell_count := g_cell_count + 1 ;
    g_cells( g_cell_count ).r := p_row ;
    g_cells( g_cell_count ).c := p_column ;
    g_cells( g_cell_count ).v := p_value ;
    g_cells( g_cell_count ).w := p_worksheet_name ;
    g_cells( g_cell_count ).s := p_style ;
    g_cells( g_cell_count ).dt := 'Number' ;
    END ;
    PROCEDURE write_cell_null(p_row NUMBER , p_column NUMBER , p_worksheet_name IN VARCHAR2, p_style IN VARCHAR2 ) IS
    BEGIN
    -- ???? NULL IS allowed here FOR time being. one OPTION IS TO warn USER that NULL IS passed but otherwise
    -- the excel generates without error
    g_cell_count := g_cell_count + 1 ;
    g_cells( g_cell_count ).r := p_row ;
    g_cells( g_cell_count ).c := p_column ;
    g_cells( g_cell_count ).v := null ;
    g_cells( g_cell_count ).w := p_worksheet_name ;
    g_cells( g_cell_count ).s := p_style ;
    g_cells( g_cell_count ).dt := NULL ;
    END ;
    PROCEDURE set_row_height( p_row IN NUMBER , p_height IN NUMBER, p_worksheet IN VARCHAR2 ) IS
    BEGIN
    g_ROW_count := g_ROW_count + 1 ;
    g_rows( g_row_count ).r := p_row ;
    g_rows( g_row_count ).ht := p_height ;
    g_rows( g_row_count ).w := p_worksheet ;
    END ;
    PROCEDURE set_column_width( p_column IN NUMBER , p_width IN NUMBER, p_worksheet IN VARCHAR2 ) IS
    BEGIN
    g_column_count := g_column_count + 1 ;
    g_columns( g_column_count ).c := p_column ;
    g_columns( g_column_count ).wd := p_width ;
    g_columns( g_column_count ).w := p_worksheet ;
    END ;
    END ;
    SHOW errors ;
    Thanks,
    Maddy B

    Hi,
    Peter Gjelstrup wrote:
    Next thing is how to use it:
    declare
    k_file constant varchar2(30) := 'YourFile.xls'
    begin
    create_file(k_file);
    -- Call other procedures
    close_file;
    end;
    Don't forget: these procedures are all part of the gen_xl_xml package.
    To call them from outside the package, you have to prefix each procedure name with the package name, like this:
    declare
    k_file constant varchar2(30) := 'YourFile.xls'
    begin
    gen_xl_xml.create_file(k_file);
    -- Call other procedures
    gen_xl_xml.close_file;
    end;

  • Dbms_xmlgen

    Dear Sir,
    dbms_xmlgen This is the predefined package which is used to generate the xml file in l0g . Is there any predefined package is there for generating the excel file . If it is there which package will help me to generate excel file through predefined packages in 10g.
    Please guide me i will do rest of the part
    Regards,
    Chandrasekhar B.S.

    sir
    gen_xl_xml package is create, but i can not execute query
    what can i do plese help
    DECLARE
    r NUMBER := 0 ;
    BEGIN
    gen_xl_xml.create_excel('UTL_DIR', 'employees.xls') ;
    gen_xl_xml.create_worksheet( 'sheet1');
    -- gen_xl_xml.create_excel_apps ;
    gen_xl_xml.create_style( 'sgs1' , 'Courier', 'red',16, TRUE , p_backcolor => 'LightGray', P_underline => 'Single' );
    gen_xl_xml.create_style( 'sgs2' , 'Courier', 'blue',12,NULL );
    gen_xl_xml.create_style( 'sgs3' , 'Courier', 'green',14,TRUE );
    -- increase width OF colum b that IS no 2
    gen_xl_xml.set_column_width( 1, 140, 'sheet1' );
    gen_xl_xml.set_column_width( 2, 145 , 'sheet1' );
    gen_xl_xml.set_column_width( 3, 145, 'sheet1' );
    gen_xl_xml.set_row_height( 1, 30 ,'sheet1' );
    -- writing the headers
    r := r+1 ;
    gen_xl_xml.write_cell_char( r,1, 'sheet1', 'empno' ,'sgs1' );
    gen_xl_xml.write_cell_char( r,2, 'sheet1', 'ename' ,'sgs1' );
    gen_xl_xml.write_cell_char( r,3, 'sheet1', 'sal', 'sgs1' );
    -- gen_xl_xml.write_cell_char( r,3, 'Last Name', 'sgs1' );
    FOR rec IN (SELECT empno , ename , sal FROM emp where ROWNUM < 10 ) LOOP
    r := r+1 ;
    gen_xl_xml.write_cell_num( r,1, 'sheet1' , rec.empno, 'sgs3' );
    gen_xl_xml.write_cell_char( r,2, 'sheet1' , rec.ename, 'sgs2' );
    gen_xl_xml.write_cell_char( r,3, 'sheet1' , rec.sal, 'sgs2');
    END LOOP ;
    gen_xl_xml.close_file ;
    END ;
    Error on line 0
    DECLARE
    r NUMBER := 0 ;
    BEGIN
    gen_xl_xml.create_excel('UTL_DIR', 'employe
    ORA-20101: UTL_FILE raised invalid path, check the directory passed is correct and you have access to it.
    ORA-06512: at "SCOTT.GEN_XL_XML", line 142
    ORA-06512: at line 4
    sir pls help

  • Utl_file not generating file but no raising no exceptions

    The utl_file_dir is set to /tmp/usr on my system. I am trying to write a simple file in this directory as follows :
    declare
    l_filehandle UTL_FILE.FILE_TYPE;
    begin
    l_filehandle := UTL_FILE.FOPEN('/usr/tmp', 'test_file','w');
    utl_file.putf(l_filehandle, 'test');
    utl_file.fclose(l_filehandle);
    end;
    When i run this, i do not see any errors but at the same time the file is not generated. Any ideas what the issue may be.
    I ran this from both sqlplus client and from the unix server but no luck in both teh cases. The procedure just completes without generating the file.
    please help
    thanks
    Satya

    thanks for the response, apparantly it was creating the files on a different server. I'm not sure why or how.
    But thanks, problem solved !!

  • Problems with moving files to ora directory UTL_FILE.PUT_RAW - ORA-29285

    hi,
    i'm using apex 4.1
    i have a procedure which moves my file from apex_application_files to ORA directory.
    if i choose a text file or small word document which is 1kb, it works. but if i have pdf file (85kb) or word document (16kb) it gives me ORA-29285: file write error
    what's my problem?
    PROCEDURE put_file_to_server (p_filename IN VARCHAR2,p_cert_type IN VARCHAR2,p_cert_pk IN NUMBER)
    AS
    l_file UTL_FILE.file_type;
    l_blob_len INTEGER;
    l_pos INTEGER := 1;
    l_amount BINARY_INTEGER := 32767;
    l_buffer RAW (32767);
    v_new_filename VARCHAR2(100);
    v_bfile BFILE ;
    BEGIN
    -- delete from apex_application_files;
    --Neuen Dateinamen generieren
    v_new_filename := p_cert_type||'_'||p_cert_pk;
    v_bfile := BFILENAME (v_directory, v_new_filename);
    --Datei erstellen
    l_file := UTL_FILE.fopen(v_directory,v_new_filename,'w');
    IF DBMS_LOB.FILEEXISTS (v_bfile) = 1 THEN
    cert_log_pkg.m(p_module => 'CERT_FILE_PKG.PUT_FILE_TO_SERVER',p_msg => 'File exists');
    FOR rec IN (select blob_content lblob from apex_application_files where rownum = 1)
    LOOP
    l_blob_len := DBMS_LOB.getlength(rec.lblob);
    cert_log_pkg.m(p_module => 'CERT_FILE_PKG.PUT_FILE_TO_SERVER',p_msg => 'Filesize is '||l_blob_len);
    WHILE l_pos < l_blob_len LOOP
    DBMS_LOB.read (rec.lblob, l_amount, l_pos, l_buffer);
    UTL_FILE.put_raw (l_file, l_buffer, FALSE);
    l_pos := l_pos + l_amount;
    END LOOP;
    COMMIT;
    END LOOP;
    --Datei schließen
    UTL_FILE.fclose(l_file);
    else
    cert_log_pkg.m(p_module => 'CERT_FILE_PKG.PUT_FILE_TO_SERVER',p_msg => 'Datei doesn't exist');
    end if;
    EXCEPTION
    WHEN OTHERS
    THEN
    -- Close the file if something goes wrong.
    IF UTL_FILE.is_open (l_file) THEN
    UTL_FILE.fclose (l_file);
    END IF;
    delete from apex_application_files;
    RAISE;
    delete from apex_application_files;
    END put_file_to_server;

    Sorry but din't test this...Can you give it a try and see if this works?
    PROCEDURE put_file_to_server(
        p_filename  IN VARCHAR2,
        p_cert_type IN VARCHAR2,
        p_cert_pk   IN NUMBER)
    AS
      l_file UTL_FILE.file_type;
      l_blob_len INTEGER;
      l_pos      INTEGER      := 1;
      l_amount BINARY_INTEGER := 32767;
      l_buffer RAW (32767);
      v_new_filename VARCHAR2(100);
      v_bfile BFILE ;
      vblob BLOB;
      vstart NUMBER := 1;
      my_vr RAW(32000);
      bytelen NUMBER := 32000;
      LEN     NUMBER;
    BEGIN
      -- delete from apex_application_files;
      --Neuen Dateinamen generieren
      v_new_filename := p_cert_type||'_'||p_cert_pk;
      v_bfile        := BFILENAME (v_directory, v_new_filename);
      --Datei erstellen
      --l_file                          := UTL_FILE.fopen(v_directory,v_new_filename,'w');
      l_file                          := UTL_FILE.fopen(v_directory,v_new_filename, 'WB', 32760);
      IF DBMS_LOB.FILEEXISTS (v_bfile) = 1 THEN
        cert_log_pkg.m(p_module => 'CERT_FILE_PKG.PUT_FILE_TO_SERVER',p_msg => 'File exists');
        FOR rec IN
        (SELECT blob_content lblob,
          LENGTH(blob_content) LEN
        FROM apex_application_files
        WHERE rownum = 1
        LOOP
          cert_log_pkg.m(p_module => 'CERT_FILE_PKG.PUT_FILE_TO_SERVER',p_msg => 'Filesize is '|| LEN);
          IF LEN < 32760 THEN
            utl_file.put_raw(l_file,lblob);
            utl_file.fflush(l_file);
          ELSE -- write in pieces
            vstart      := 1;
            WHILE vstart < LEN
            LOOP
              dbms_lob.read(vblob,bytelen,vstart,my_vr);
              utl_file.put_raw(l_file,my_vr);
              utl_file.fflush(l_file);
              -- set the start position for the next cut
              vstart := vstart + bytelen;
              -- set the end position if less than 32000 bytes
              x         := x - bytelen;
              IF x       < 32000 THEN
                bytelen := x;
              END IF;
            END LOOP;
          END IF;
         END LOOP;
        ELSE
          cert_log_pkg.m(p_module => 'CERT_FILE_PKG.PUT_FILE_TO_SERVER',p_msg => 'Datei doesnt exist');
        END IF;
        utl_file.fclose(l_file);
      EXCEPTION
      WHEN OTHERS THEN
        -- Close the file if something goes wrong.
        IF UTL_FILE.is_open (l_file) THEN
          UTL_FILE.fclose (l_file);
        END IF;
        DELETE FROM apex_application_files;
        RAISE;
        DELETE FROM apex_application_files;
      END put_file_to_server;Edited by: Vitor Rodrigues on 17/Fev/2012 12:03

  • How do I use UTL_FILE to insert a large number of fields to a file?

    Hi
    I am trying to use UTL_FILE for the first time in a Stored Procedure. I need to run a complex query to select 50 fields from various tables. I need these to be inserted into one line in the output file for all rows. Is this possible? My procedure so far is like the following
    CREATE OR REPLACE PROCEDURE PROC_TEST IS
    output_file UTL_FILE.FILE_TYPE;
    BEGIN
    FOR query in (SELECT FIELD1, FIELD2, ..........FIELD50)
    FROM TABLE A, TABLE B
    WHERE A.ID = B.ID
    ETC
    LOOP
    UTL_FILE.PUT_LINE(output_file, <put all 50 fields for all records into file> );
    END LOOP;               
    UTL_FILE.FCLOSE (output_file);
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    NULL;
    WHEN OTHERS THEN
         UTL_FILE.FCLOSE_ALL;
    RAISE;
    END PROC_TEST;
    Do I need to define 'query' (after the FOR) anywhere, also please advise with how I put all of the fields into the file.
    Thanks
    GB

    Thanks Steve,
    I have the UTL_FILE working fine now.
    I have other queries to run and conditions to apply in the same procedure, and I need to schedule via Enterprise Manager, therefore using UTL_FILE in a procedure seemed the best option. I looked up Data-pump but this seems to be an 11g feature, and we are still on 10g therefore I will not be able to use it.
    Thanks for your help.
    GB

  • Error in the 10.2 and 9.2 UTL_FILE documentation

    The 10.2 and 9.2 documentation for the utl_file.fcopy procedure provide the wrong names for the first two parameters
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/u_file.htm#i1003400
    The 11.2 documentation correctly notes that the names of the first two parameters are SRC_LOCATION and SRC_FILENAME. But the 10.2 documentation refers to them as LOCATION and FILENAME. I've verified in a 10.2 database that the parameter names are actually SRC_LOCATION and SRC_FILENAME. I assume that the parameter names were also SRC_LOCATION and SRC_FILENAME in 9.2 but I don't have a 9.2 instance to test with.
    Justin

    Thanks for noting this specific case and raising the general issue of errors in books for "old" releases. In most cases (including, I would think, this one), acknowledging the error and fixing it for the current release and future ones is sufficient.
    The following statement has been approved by some people in Oracle Database documentation management: "We usually make corrections only for the next revision of the book for the most current available release and for upcoming releases. If there is a strong business need to make corrections also to published versions of the book for previous releases, we consider that on a case by case basis."

  • Writing files using UTL_FILE package

    Greetings:
    I'm attempting to write simple text files on the server side using the UTL_FILE package.I can get it to write the file to a local drive on the Oracle server with no problems, but not a network drive. When trying to write to a network drive, the FOPEN function raises the UTL_FILE.INVALID_OPERATION exception. This is even with my UTL_FILE_DIR parameter set the * for all directories and I have "Full Control" permission on the directory. I am running in a NT Server/Wkstn environment. Anyone have any ideas why I can't write a file to a network drive?
    Thanks a lot,
    Chris Scopp

    Thanks for your response...
    I have set the UTL_FILE_DIR parameter... I've tried setting it to the * for all directories and also mapping a drive letter from the server to where I want to write the file and then explicitly naming this path in the UTL_FILE_DIR parameter. Neither works, I still get the INVALID_OPERATION exception raised on the FOPEN function. I'm convinced now that it does have something to do with NT because I have been able to do the same operation writing to a Win95/98 box and it works fine. I have "Full Control" to all places I'm trying to write to, any other ideas?
    Thanks a lot,
    Chris Scopp

  • UTL_FILE - File Size limit does not exceed 32990 bytes

    Hi,
    We are using UTL_FILE package of oracle for writing data into a file.We are using "UTL_FILE.FOPEN" function to open the file in the write mode and "UTL_FILE.PUT" function to write data into it.
    We have specified the max size of the record as 32767 bytes as one of the parameters to the fopen function.
    Oracle : 8.1.6
    Platform : NT
    Problem:
    The amount of data written to the file is incomplete. The number of bytes written to the file is always 32990 bytes and it does not write the remaining data. Can u please explain this.
    When using "UTL_FILE.PUT_LINE" function, the entire data is written to the file.However I cannot use this because this function inserts a new line characte as the line terminator which is not our requirement..
    I would appreciate your replies in this regard.
    null

    It's a DBMS_OUTPUT error. You are restricted to 255 bytes when using it to output data. From Note: 1010445.6 on MetaLink:
    Problem Description
    You receive the following errors when trying to use
    DBMS_OUTPUT.PUT_LINE() to output a string > 255 characters:
    ORA-20000 a package-specific exception raised
    ORU-10028 line length overflow, limit of 255 bytes per line
    Solution Description
    This is a restriction of the package DBMS_OUTPUT(). The restriction
    and resulting error messages are documented in the file dbmsotpt.sql.
    This file can be found in the directory $ORACLE_HOME/rdbms/admin
    on UNIX platforms.

  • Ask about UTL_FILE in Stored Procedure

    Hi
    I was trying to do an insert in a table which then trigger a stored procedure to write the inserted data to a text file. However, I have some issues here in the Stored Procedure. Thanks for trying to help... [Insert -> Table -> Trigger -> Stored Procedure -> Text File]
    ----User will execute this sql
    ----insert into tester.test_table values ('ab');
    CREATE TABLE TESTER.TEST_TABLE
      LINECODE  VARCHAR2(2 BYTE)
    ----Just for testing purpose
    CREATE TABLE TESTER.TEST_REC_TABLE
      LINECODE  VARCHAR2(2 BYTE)
    CREATE OR REPLACE TRIGGER TESTER.TRIGGER_AUDIT
    AFTER INSERT OR UPDATE
    ON TESTER.TEST_TABLE REFERENCING OLD AS OLD NEW AS NEW
    FOR EACH ROW
    DECLARE
      sLineCode VARCHAR2(2);
    BEGIN
       sLineCode := '';
        IF UPDATING THEN     
          sLineCode := :NEW.LINECODE;
          TRIGGER_PACKAGE.WRITE_FILE(sLineCode);
        END IF;  
        IF INSERTING THEN
          sLineCode := :NEW.LineCode;
          TRIGGER_PACKAGE.WRITE_FILE(sLineCode);
        END IF;
    EXCEPTION
        WHEN others then null;
    END TRIGGER_AUDIT;
    CREATE OR REPLACE PACKAGE BODY TESTER.TRIGGER_PACKAGE
    is
       procedure WRITE_FILE(in_LineCode in varchar2)
       is
         sLineCode varchar2(2);  
         v_FileHandle UTL_FILE.FILE_TYPE;
    ----some checking of the input data for NULL 
       begin  
        if ((rtrim(in_LineCode) IS NULL) or (in_LineCode is null)) then
            sLineCode := 'XX';
        else
            sLineCode := in_LineCode;          -------->PROBLEM HERE?
        end if;
        if (length(sLineCode) > 2) then
          sLineCode := substr(sLineCode,1,2);
        end if;
        Insert into TEST_REC_TABLE (LineCode) values (sLineCode); ----insert data to another table for testing
        EXECUTE IMMEDIATE 'CREATE OR REPLACE DIRECTORY FILEDIR AS ' || '''' || 'c:\' || '''';
        v_FileHandle := UTL_FILE.FOPEN('FILEDIR','Test_Table.txt','w'); ----write to text file in C:\Test_Table.txt
        UTL_FILE.PUT_LINE(v_FileHandle, 'Inserted ' || sLineCode || ' @ ' || TO_CHAR(SYSDATE,'DD-MM-YYYY HH:MI:SS AM'));
        UTL_FILE.FCLOSE(v_FileHandle);           
       exception
          when others then null;
       end;
    end TRIGGER_PACKAGE;
    1) The problem I faced is that when I perform INSERT sql for the new data, it can be successfully be inserted into both tables TEST_REC_TABLE and TEST_TABLE, but it just does not write to the text file.
    2) However, when I just execute the procedure I will get XX for the input data as it is NULL. This XX data will then be seen in TEST_REC_TABLE as "XX" record and Test_Table.txt as "Inserted XX @ 10-01-2014 04:56:06 PM". UTL_FILE did write to textfile so my guess is that it could be due to my poor understanding of logic in the checking of the input data for NULL. Tried but I am still clueless, thanks for pointing out the issue.

    > 1) I'm just testing and learning here with trigger insert...
    Good. In that case its just a wrong choice that you have decided to write to a file from trigger. Oracle base is a good site. They have examples too. Please check that ORACLE-BASE - Database Triggers Overview.
    > 2) How should I do then?
    Just create the object one time
    CREATE OR REPLACE DIRECTORY FILEDIR AS <path>
    The path should be a location in your server where oracle is installed.
    > 3) How should I do then? Use this ?
    EXCEPTION
          WHEN OTHERS THEN
               DBMS_OUTPUT.PUT_LINE
                    ('ERROR ' || TO_CHAR(SQLCODE) || SQLERRM);
               NULL;
    Don't do any thing. Just remove the exception handling. Don't handle unknown exceptions. If at all you want to handle them for logging purpose RAISE it at the end like this.
    exception
      when others then
        <log your messages>
        raise;
    And about exception handling one of the forum member BluShadow has written a nice article PL/SQL 101 : Exception Handling. That would be a nice read.

  • Two message in different files widout help of UTL_FILE

    Hi All..happy new year in advance..!
    I am in a strange scenerio by my client.I am having below type of code in which I am writing log via
    "dbms_output.put_line" but he needs that whenevr any error came into code eror message should be written in separate file but due to some ftp permission we cant use UTL_FILE option.
    kind ly give me any idea,i hav done much r&d..help..!
    spool msg_log.log
    declare
    raise_pc1 exception;
    raise_pc2 exception;
    begin
    dbms_output.put_line('hi..this msg is from code block.');
    raise raise_pc1;
    exception
    when raise_pc1 then
    dbms_output.put_line('hi..this msg is from exception block.');
    end;
    spool off
    exit;
    o/p is- hi..this msg is from code block.
    hi..this msg is from exception block.
    above o/p is coming in same log file.
    I need that 1st line should be in 1 file(ex-msg_log.log) &
    2nd eror line should be in 2nd log file (ex-error_log.log)
    rgds,
    pc

    Hi AP,
    In this case everytime, when I will get exception in any step only 1 message will print bcoz my_stat = 1 for every exception & in 2nd block we have same 1 line to print.Kindly look ionto it also ..
    var my_stat NUMBER;
    SET SERVEROUTPUT ON
    spool msg_log.log;
    declare
         raise_pc1 exception;
         raise_pc2 exception;
    begin
         dbms_output.put_line('hi..this msg is from code block.');
         begin
              dbms_output.put_line('hi..raised raise_pc1.');
              raise raise_pc1;
         exception
              when raise_pc1 then
              :my_stat := 1;
         end;
         begin
              dbms_output.put_line('hi..raised raise_pc2.');
              raise raise_pc2;
         exception
              when raise_pc2 then
              :my_stat := 1;
         end;
    end;
    spool off;
    spool err_log.log;
    begin
         if (:my_stat = 1) THEN
              dbms_output.put_line('hi..this msg is from exception block.'||sqlerrm);
         end if;
    end;
    spool off;
    exit;
    & o/p is --
    hi..this msg is from exception block.ORA-0000: normal, successful completion
    PL/SQL procedure successfully completed.
    here I am getting only 1 line in o/p even 2 exception had raised..
    rgds,
    pc

  • Query on UTL_FILE

    Hi All,
    I have a procedure which generates a file using UTL_FILE package. If the procedure fails while generating the file, will ORACLE cloase the file after the procedure fails?
    Thanks in advance,
    Annie

    user10660677 wrote:
    Continuing on this discussion, is there a way to check the file is open when the procedure is called once again after it has failed.
    To make it more clear, is it possible to check whether the file was left open from the last time the procedure was called. If yes, then close it and try a fresh open.Only if you still have the file handle, which typically you won't as you will have declared it locally within the procedure.
    If however, it was a package variable and you were still connected within the same database session then the package variable would still hold the file handle.
    However, I wouldn't recommend doing it that way because it's leaving it open to problems. Far better to capture the exception, close the file if it's open and then raise the exception up.

  • Problem in UTL_FILE

    I have written a following procedure and it complies successfully.
    create or replace procedure utl_file_test_write (
    path in varchar2,
    filename in varchar2,
    firstline in varchar2,
    secondline in varchar2)
    is
    output_file utl_file.file_type;
    begin
    output_file := utl_file.fopen (path,filename, 'W');
    utl_file.put_line (output_file, firstline);
    utl_file.put_line (output_file, secondline);
    utl_file.fclose(output_file);
    end;
    I have create the directory using the following command
    SQL> CREATE DIRECTORY log_dir AS 'd:\oracle\oraclelog_dir';
    Directory created
    SQL> GRANT READ ON DIRECTORY log_dir TO DBA;
    Grant succeeded
    SQL> CREATE DIRECTORY out_dir AS 'd:\oracle\oracleout_dir';
    Directory created
    SQL> GRANT READ ON DIRECTORY out_dir TO DBA;
    Grant succeeded
    SQL> COMMIT;
    But when I execute this then it gives the following error.
    ORA-06510: PL/SQL: unhandled user-defined exception
    ORA-06512: at "SYS.UTL_FILE", line 98
    ORA-06512: at "SYS.UTL_FILE", line 157
    ORA-06512: at "UTL_FILE_TEST_WRITE", line 9
    ORA-06512: at line 1
    I am using the following version.
    Oracle8i Enterprise Edition Release 8.1.5.0.0 - Production
    PL/SQL Release 8.1.5.0.0 - Production
    CORE Version 8.1.3.0.0 - Production
    TNS for Solaris: Version 8.1.5.0.0 - Production
    NLSRTL Version 3.4.0.0.0 - Production
    Can anybody help me how can I solve the problem.
    Thanks in advance

    I think you are not handeling the exceptions. First you add the exceptions and it will tell you what kind of exception is raised.
    Try:
      EXCEPTION
           WHEN utl_file.invalid_path THEN
             raise_application_error(-20100,'Invalid Path');
           WHEN utl_file.invalid_mode THEN
             raise_application_error(-20101,'Invalid Mode');
           WHEN utl_file.invalid_operation THEN
             raise_application_error(-20102,'Invalid Operation');
           WHEN utl_file.invalid_filehandle THEN
             raise_application_error(-20103,'Invalid Filehandle');
           WHEN utl_file.write_error THEN
             raise_application_error(-20104, 'Write Error');
           WHEN utl_file.read_error THEN
             raise_application_error(-20105,'Read Error');
           WHEN utl_file.internal_error THEN
             raise_application_error(-20106,'Internal Error');
           WHEN others THEN
             utl_file.fclose(v_file_id);
             raise_application_error (-20007, sqlerrm);HTH
    Ghulam

  • Cannot create a file using  UTL_FILE.FOPEN

    Dear All,
    I am using this syntax for creating a file in window
    i have declare this like this
    l_file_id UTL_FILE.file_type;
    l_file_name := 'DHL_'||110570284||'_'||TO_CHAR(SYSDATE,'ddmmyyyy')||'.txt'
    l_file_id :=UTL_FILE.FOPEN('C:\D2R',l_file_name,'W');
    Is this syntax work?
    My database version is oralce 10g
    Thanks

    First of all, UTL_FILE is PL/SQL package and is executed on database server side, not on client side. Therefore, it is trying to create file in directory C:\D2R on database sefrver. So if you want to create file in directory C:\D2R on your client box - you can't. Next point - you are specifying directory explicitly which old and obsolete syntax. It requires, for security reasons to add C:\D2R to init.ora parameter UTL_FILE_DIR, otherwise UTL_FILE will raise an error. You should use directory objects:
    CREATE DIRECTORY dir AS 'C:\D2R';and grant READ/WRITE on it to users. Then use:
    l_file_id :=UTL_FILE.FOPEN('DIR',l_file_name,'W'); -- use directory name in upper caseAnd keep in mind, as I already mentioned UTL_FILE is PL/SQL package and is executed on database server side which means it runs under OS user oracle. Therefore OS user oracle must have write permission on C:\D2R.
    SY.

  • Reading UTL_File, first record is missing

    Hi,
    I am reading text file using UTL_FILE to insert data in staging table.For some reason the first record in the text file in not inserting in table
    (suppose there are 10 records in text.file, except very first record rest of the records will be inserted successfully).Please help me on this.
    create or replace
    PROCEDURE PRC_XYZ_FILE_LOAD(
        p_in_file_name IN VARCHAR2,
        p_out_message OUT VARCHAR2)
    IS
    v_membernumber     whr_stageftd.membernumber%TYPE ;
      v_memberfullname   whr_stageftd.memberfullname%TYPE ;
    BEGIN
    --open files
      v_file_handler := UTL_FILE.FOPEN(v_file_path, v_file_name, 'r');
       UTL_FILE.GET_LINE(v_file_handler, v_text);
       LOOP
        BEGIN
             BEGIN
            UTL_FILE.GET_LINE(v_file_handler, v_text);
          EXCEPTION
          WHEN no_data_found THEN
            EXIT; -- end of file
          END;
          -------------------- Insert data in to whr_stageftd table -------------------------------
          v_detail_record              :=FNC_PIPE_PARSE (V_TEXT,1,'~');
           v_membernumber               :=FNC_PIPE_PARSE (V_TEXT,2,'~');
           v_memberfullname             :=FNC_PIPE_PARSE (V_TEXT,3,'~');
           v_stage:='stage3';
           v_locationid                 :=FNC_PIPE_PARSE (V_TEXT,4,'~');
           v_regionid                   :=FNC_PIPE_PARSE (V_TEXT,5,'~');
           v_transstartdate             :=FNC_PIPE_PARSE (V_TEXT,6,'~');
           v_transenddate               :=FNC_PIPE_PARSE (V_TEXT,7,'~');
          IF v_detail_record            = '2' THEN
            INSERT
            INTO whr_stageftd
                fileid ,
                membernumber ,
                memberfullname ,
                locationid,
                regionid,
                earningstartdate,
                earningenddate  )
              VALUES
                v_fileid,
                v_membernumber,
                v_memberfullname,
                v_locationid,
                v_regionid,
                v_transstartdate ,
                v_transenddate);
            v_rec_counter := v_rec_counter + 1;
            IF mod(v_rec_counter,10000)= 0 THEN
              COMMIT;
            END IF;
          END IF;
        EXCEPTION
        WHEN OTHERS THEN
         dbms_output.put_line(Sqlerrm||v_stage);
          END;
      END LOOP;
       COMMIT;
      UTL_FILE.FCLOSE(v_file_handler);
    END PRC_XYZ_FILE_LOAD;here is the code for FNC_PIPE_PARSE
    create or replace
    Function      FNC_PIPE_PARSE
    (p_instring in varchar2,
      p_field_no in number,
      p_delimiter in varchar2 default '|')
      RETURN varchar2 IS
    v_start number;
    v_no_of_chars number;
    v_instring varchar2(4000);
        v_delimiter_length NUMBER(10) := 0;
    BEGIN
    v_delimiter_length := length(p_delimiter);
    v_instring:=p_instring||p_delimiter;
      IF p_field_no = 1 THEN
         return(substr(v_instring,1,instr(v_instring,p_delimiter)-1));
      ELSE
       v_start := instr(v_instring,p_delimiter,1,p_field_no-1)+v_delimiter_length;
       v_no_of_chars := instr(v_instring,p_delimiter,1,p_field_no) - v_start;
    return(substr(v_instring,v_start,v_no_of_chars));
      END IF;
    END; sample data
    actual data
    2~105266648J~C TEST~~~20050707~20050707
    2~104133506D~K TEST~~~20050707~20050707
    2~104929890I~A TEST~~~20050707~20050707
    2~104700166K~D TEST~~~20050708~20050708data inserted in table is (which is missing first record
    2~104133506D~K TEST~~~20050707~20050707
    2~104929890I~A TEST~~~20050707~20050707
    2~104700166K~D TEST~~~20050708~20050708Sqlerrm error is
    ORA-06502: PL/SQL: numeric or value error: character string buffer too small stage3THANKS
    Sandy

    Apparently, you don't specify the len parameter to GET_LINE, and the doc says :
    The GET_LINE len parameter must be a number in the range 1 and 32767. If unspecified, Oracle supplies the default value of max_linesize.The FOPEN max_linesize parameter must be a number in the range 1 and 32767. If unspecified, Oracle supplies a default value of 1024. Could that be the culprit ?You should try to put some debug in your code to see what line content raises the error.

Maybe you are looking for

  • In Sales order- Extras- packing proposal screen ,HU Sorting problem.

    HI All, In VA01/Va02/VA03 sales order screen-> Extras->packing proposal screen, handling units were shown on the basis of alphabetical sorting of packaging material field. (Handling units are getting sorted on the basis of packaging material field) A

  • Track Purchases/payments Sales/Receipts for 2 business units in 1 OU

    Hi, We have one Operating unit and need to capture the liabilities/payables for 2 different business units. What approach can be undertaken to set up the liability account ? Should we maintain 2 different liability accounts ? If the same supplier dea

  • Can an image of Windows 2000 using NI-VXI 3.5 with a PXI-MXI controller be fielded for multiple computers using sysprep?

    I am trying to field an image of Windows 2000 using NI-VXI 3.5 with a PXI-MXI controller.  Can I set the system up to retain to original settings for VXI?  Some of my computers use a PCI-MXI controller.  Although Windows always finds the MXI controll

  • Doclet

    HI, I need to extend the Standard doclet to support a few new tags. In each generated class page, I'm going to print the full text of the tags with the method they apply to, print a summary of info in the tags in the class description area, and suppo

  • Error Message, Won't Install from Disc 1

    The MacBook started to act up two days ago and became hot. (It was unusually slow, too) After being shut down and left alone for a while, I tried to turn it on. It would sit at the gray Apple start-up screen then would show this screen (http://flickr