How to specify max number of rows to be written in one csv file in oracle?












0














I am using the procedure below as mentioned in this to dump data into csv.
This writes data into a single csv file test.csv. In case table is very big, I don't want to write into one csv file. I want to specify a threshold of 10k into it with a file name as




test_sequenceNumber _timeOfOpeningInYYYYMMDDHHMMSSsss.csv




so for example if row count of a table is 15k then I should have 2 files:



test_1_20181112012830000.csv 
test_2_20181112012835005.csv


How can we I achieve this by modifying the below SP?



create or replace procedure dump_table_to_csv
( p_tname in varchar2
, p_dir in varchar2
)
as
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_query varchar2(1000)
default 'select * from ' || p_tname;
l_colCnt number := 0;
l_separator varchar2(1);
l_descTbl dbms_sql.desc_tab;
p_filename varchar2(100) := 'test.csv' ;
begin
l_output := utl_file.fopen( p_dir, p_filename, 'w' , 32760);
execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';

dbms_sql.parse( l_theCursor, l_query, dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );

for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
l_separator := ',';
end loop;

utl_file.new_line( l_output );

l_status := dbms_sql.execute(l_theCursor);

while (dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
l_separator := '';

for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i, l_columnValue );
utl_file.put( l_output, l_separator || l_columnValue );
l_separator := ',';
end loop;

utl_file.new_line(l_output );
end loop;

dbms_sql.close_cursor(l_theCursor);
utl_file.fclose(l_output );

execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
exception
when others then
utl_file.fclose( l_output );
execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
raise;
end;


EDIT1:
I was trying of applying a logic of mod(num_rows_written,10000) == 0 it's time to close old file and start a new one but just stuck at a point that, what should be top most loop to continue the same operation on new file ?










share|improve this question
























  • Any reason for down-voting ? The solution of this problem must be useful for many.
    – Laxmikant
    Nov 10 at 21:20


















0














I am using the procedure below as mentioned in this to dump data into csv.
This writes data into a single csv file test.csv. In case table is very big, I don't want to write into one csv file. I want to specify a threshold of 10k into it with a file name as




test_sequenceNumber _timeOfOpeningInYYYYMMDDHHMMSSsss.csv




so for example if row count of a table is 15k then I should have 2 files:



test_1_20181112012830000.csv 
test_2_20181112012835005.csv


How can we I achieve this by modifying the below SP?



create or replace procedure dump_table_to_csv
( p_tname in varchar2
, p_dir in varchar2
)
as
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_query varchar2(1000)
default 'select * from ' || p_tname;
l_colCnt number := 0;
l_separator varchar2(1);
l_descTbl dbms_sql.desc_tab;
p_filename varchar2(100) := 'test.csv' ;
begin
l_output := utl_file.fopen( p_dir, p_filename, 'w' , 32760);
execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';

dbms_sql.parse( l_theCursor, l_query, dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );

for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
l_separator := ',';
end loop;

utl_file.new_line( l_output );

l_status := dbms_sql.execute(l_theCursor);

while (dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
l_separator := '';

for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i, l_columnValue );
utl_file.put( l_output, l_separator || l_columnValue );
l_separator := ',';
end loop;

utl_file.new_line(l_output );
end loop;

dbms_sql.close_cursor(l_theCursor);
utl_file.fclose(l_output );

execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
exception
when others then
utl_file.fclose( l_output );
execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
raise;
end;


EDIT1:
I was trying of applying a logic of mod(num_rows_written,10000) == 0 it's time to close old file and start a new one but just stuck at a point that, what should be top most loop to continue the same operation on new file ?










share|improve this question
























  • Any reason for down-voting ? The solution of this problem must be useful for many.
    – Laxmikant
    Nov 10 at 21:20
















0












0








0







I am using the procedure below as mentioned in this to dump data into csv.
This writes data into a single csv file test.csv. In case table is very big, I don't want to write into one csv file. I want to specify a threshold of 10k into it with a file name as




test_sequenceNumber _timeOfOpeningInYYYYMMDDHHMMSSsss.csv




so for example if row count of a table is 15k then I should have 2 files:



test_1_20181112012830000.csv 
test_2_20181112012835005.csv


How can we I achieve this by modifying the below SP?



create or replace procedure dump_table_to_csv
( p_tname in varchar2
, p_dir in varchar2
)
as
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_query varchar2(1000)
default 'select * from ' || p_tname;
l_colCnt number := 0;
l_separator varchar2(1);
l_descTbl dbms_sql.desc_tab;
p_filename varchar2(100) := 'test.csv' ;
begin
l_output := utl_file.fopen( p_dir, p_filename, 'w' , 32760);
execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';

dbms_sql.parse( l_theCursor, l_query, dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );

for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
l_separator := ',';
end loop;

utl_file.new_line( l_output );

l_status := dbms_sql.execute(l_theCursor);

while (dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
l_separator := '';

for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i, l_columnValue );
utl_file.put( l_output, l_separator || l_columnValue );
l_separator := ',';
end loop;

utl_file.new_line(l_output );
end loop;

dbms_sql.close_cursor(l_theCursor);
utl_file.fclose(l_output );

execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
exception
when others then
utl_file.fclose( l_output );
execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
raise;
end;


EDIT1:
I was trying of applying a logic of mod(num_rows_written,10000) == 0 it's time to close old file and start a new one but just stuck at a point that, what should be top most loop to continue the same operation on new file ?










share|improve this question















I am using the procedure below as mentioned in this to dump data into csv.
This writes data into a single csv file test.csv. In case table is very big, I don't want to write into one csv file. I want to specify a threshold of 10k into it with a file name as




test_sequenceNumber _timeOfOpeningInYYYYMMDDHHMMSSsss.csv




so for example if row count of a table is 15k then I should have 2 files:



test_1_20181112012830000.csv 
test_2_20181112012835005.csv


How can we I achieve this by modifying the below SP?



create or replace procedure dump_table_to_csv
( p_tname in varchar2
, p_dir in varchar2
)
as
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_query varchar2(1000)
default 'select * from ' || p_tname;
l_colCnt number := 0;
l_separator varchar2(1);
l_descTbl dbms_sql.desc_tab;
p_filename varchar2(100) := 'test.csv' ;
begin
l_output := utl_file.fopen( p_dir, p_filename, 'w' , 32760);
execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';

dbms_sql.parse( l_theCursor, l_query, dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );

for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
l_separator := ',';
end loop;

utl_file.new_line( l_output );

l_status := dbms_sql.execute(l_theCursor);

while (dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
l_separator := '';

for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i, l_columnValue );
utl_file.put( l_output, l_separator || l_columnValue );
l_separator := ',';
end loop;

utl_file.new_line(l_output );
end loop;

dbms_sql.close_cursor(l_theCursor);
utl_file.fclose(l_output );

execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
exception
when others then
utl_file.fclose( l_output );
execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
raise;
end;


EDIT1:
I was trying of applying a logic of mod(num_rows_written,10000) == 0 it's time to close old file and start a new one but just stuck at a point that, what should be top most loop to continue the same operation on new file ?







oracle stored-procedures oracle11g






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 7:28

























asked Nov 10 at 20:24









Laxmikant

583313




583313












  • Any reason for down-voting ? The solution of this problem must be useful for many.
    – Laxmikant
    Nov 10 at 21:20




















  • Any reason for down-voting ? The solution of this problem must be useful for many.
    – Laxmikant
    Nov 10 at 21:20


















Any reason for down-voting ? The solution of this problem must be useful for many.
– Laxmikant
Nov 10 at 21:20






Any reason for down-voting ? The solution of this problem must be useful for many.
– Laxmikant
Nov 10 at 21:20














1 Answer
1






active

oldest

votes


















1














Well, it's a fairly straightforward task. All you need to do is close the old file and open a new one. There's no need for outer loop, all you need is to put a condition and retrace the steps for preparing the csv.



create or replace procedure dump_table_to_csv
( p_tname in varchar2
, p_dir in varchar2
)
as
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_query varchar2(1000)
default 'select * from ' || p_tname;
l_colCnt number := 0;
l_separator varchar2(1);
l_descTbl dbms_sql.desc_tab;
l_file_seq number := 1;
l_rows_written number := 0;
l_nls_format varchar2(200);
begin
l_output := utl_file.fopen( p_dir, 'test_'||l_file_seq||'_'||to_char(systimestamp, 'YYYYMMDDHH24MISSFF3'), 'w' , 32760);
l_file_seq := l_file_seq + 1;
SELECT value into l_nls_format
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT';
execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';

dbms_sql.parse( l_theCursor, l_query, dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );

for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
l_separator := ',';
end loop;

utl_file.new_line( l_output );

l_status := dbms_sql.execute(l_theCursor);

while (dbms_sql.fetch_rows(l_theCursor) > 0 ) loop

if l_rows_written >= 10000 then
utl_file.fclose( l_output );
l_output := utl_file.fopen( p_dir, 'test_'||l_file_seq||'_'||to_char(systimestamp, 'YYYYMMDDHH24MISSFF3'), 'w' , 32760);
for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
l_separator := ',';
end loop;
utl_file.new_line( l_output );

l_file_seq := l_file_seq + 1;
l_rows_written := 0;
end if;

l_separator := '';

for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i, l_columnValue );
utl_file.put( l_output, l_separator || l_columnValue );
l_separator := ',';
end loop;

utl_file.new_line(l_output );
l_rows_written := l_rows_written + 1;
end loop;

dbms_sql.close_cursor(l_theCursor);
utl_file.fclose(l_output );

execute immediate 'alter session set nls_date_format='''||l_nls_format||''' ';
exception
when others then
utl_file.fclose( l_output );
execute immediate 'alter session set nls_date_format='''||l_nls_format||''' ';
raise;
end;


It might be a good idea to offload the code for preparing CSV to a separate (sub)procedure to avoid code duplication. Also beware that original procedure has a nasty side effect of overriding your nls_date_format to dd-MON-yy without any regards to how it was set before execution; I fixed that.






share|improve this answer





















  • thanks for the answer. The logic i applied is in below answer.
    – Laxmikant
    Nov 13 at 17:49











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243090%2fhow-to-specify-max-number-of-rows-to-be-written-in-one-csv-file-in-oracle%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Well, it's a fairly straightforward task. All you need to do is close the old file and open a new one. There's no need for outer loop, all you need is to put a condition and retrace the steps for preparing the csv.



create or replace procedure dump_table_to_csv
( p_tname in varchar2
, p_dir in varchar2
)
as
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_query varchar2(1000)
default 'select * from ' || p_tname;
l_colCnt number := 0;
l_separator varchar2(1);
l_descTbl dbms_sql.desc_tab;
l_file_seq number := 1;
l_rows_written number := 0;
l_nls_format varchar2(200);
begin
l_output := utl_file.fopen( p_dir, 'test_'||l_file_seq||'_'||to_char(systimestamp, 'YYYYMMDDHH24MISSFF3'), 'w' , 32760);
l_file_seq := l_file_seq + 1;
SELECT value into l_nls_format
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT';
execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';

dbms_sql.parse( l_theCursor, l_query, dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );

for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
l_separator := ',';
end loop;

utl_file.new_line( l_output );

l_status := dbms_sql.execute(l_theCursor);

while (dbms_sql.fetch_rows(l_theCursor) > 0 ) loop

if l_rows_written >= 10000 then
utl_file.fclose( l_output );
l_output := utl_file.fopen( p_dir, 'test_'||l_file_seq||'_'||to_char(systimestamp, 'YYYYMMDDHH24MISSFF3'), 'w' , 32760);
for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
l_separator := ',';
end loop;
utl_file.new_line( l_output );

l_file_seq := l_file_seq + 1;
l_rows_written := 0;
end if;

l_separator := '';

for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i, l_columnValue );
utl_file.put( l_output, l_separator || l_columnValue );
l_separator := ',';
end loop;

utl_file.new_line(l_output );
l_rows_written := l_rows_written + 1;
end loop;

dbms_sql.close_cursor(l_theCursor);
utl_file.fclose(l_output );

execute immediate 'alter session set nls_date_format='''||l_nls_format||''' ';
exception
when others then
utl_file.fclose( l_output );
execute immediate 'alter session set nls_date_format='''||l_nls_format||''' ';
raise;
end;


It might be a good idea to offload the code for preparing CSV to a separate (sub)procedure to avoid code duplication. Also beware that original procedure has a nasty side effect of overriding your nls_date_format to dd-MON-yy without any regards to how it was set before execution; I fixed that.






share|improve this answer





















  • thanks for the answer. The logic i applied is in below answer.
    – Laxmikant
    Nov 13 at 17:49
















1














Well, it's a fairly straightforward task. All you need to do is close the old file and open a new one. There's no need for outer loop, all you need is to put a condition and retrace the steps for preparing the csv.



create or replace procedure dump_table_to_csv
( p_tname in varchar2
, p_dir in varchar2
)
as
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_query varchar2(1000)
default 'select * from ' || p_tname;
l_colCnt number := 0;
l_separator varchar2(1);
l_descTbl dbms_sql.desc_tab;
l_file_seq number := 1;
l_rows_written number := 0;
l_nls_format varchar2(200);
begin
l_output := utl_file.fopen( p_dir, 'test_'||l_file_seq||'_'||to_char(systimestamp, 'YYYYMMDDHH24MISSFF3'), 'w' , 32760);
l_file_seq := l_file_seq + 1;
SELECT value into l_nls_format
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT';
execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';

dbms_sql.parse( l_theCursor, l_query, dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );

for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
l_separator := ',';
end loop;

utl_file.new_line( l_output );

l_status := dbms_sql.execute(l_theCursor);

while (dbms_sql.fetch_rows(l_theCursor) > 0 ) loop

if l_rows_written >= 10000 then
utl_file.fclose( l_output );
l_output := utl_file.fopen( p_dir, 'test_'||l_file_seq||'_'||to_char(systimestamp, 'YYYYMMDDHH24MISSFF3'), 'w' , 32760);
for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
l_separator := ',';
end loop;
utl_file.new_line( l_output );

l_file_seq := l_file_seq + 1;
l_rows_written := 0;
end if;

l_separator := '';

for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i, l_columnValue );
utl_file.put( l_output, l_separator || l_columnValue );
l_separator := ',';
end loop;

utl_file.new_line(l_output );
l_rows_written := l_rows_written + 1;
end loop;

dbms_sql.close_cursor(l_theCursor);
utl_file.fclose(l_output );

execute immediate 'alter session set nls_date_format='''||l_nls_format||''' ';
exception
when others then
utl_file.fclose( l_output );
execute immediate 'alter session set nls_date_format='''||l_nls_format||''' ';
raise;
end;


It might be a good idea to offload the code for preparing CSV to a separate (sub)procedure to avoid code duplication. Also beware that original procedure has a nasty side effect of overriding your nls_date_format to dd-MON-yy without any regards to how it was set before execution; I fixed that.






share|improve this answer





















  • thanks for the answer. The logic i applied is in below answer.
    – Laxmikant
    Nov 13 at 17:49














1












1








1






Well, it's a fairly straightforward task. All you need to do is close the old file and open a new one. There's no need for outer loop, all you need is to put a condition and retrace the steps for preparing the csv.



create or replace procedure dump_table_to_csv
( p_tname in varchar2
, p_dir in varchar2
)
as
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_query varchar2(1000)
default 'select * from ' || p_tname;
l_colCnt number := 0;
l_separator varchar2(1);
l_descTbl dbms_sql.desc_tab;
l_file_seq number := 1;
l_rows_written number := 0;
l_nls_format varchar2(200);
begin
l_output := utl_file.fopen( p_dir, 'test_'||l_file_seq||'_'||to_char(systimestamp, 'YYYYMMDDHH24MISSFF3'), 'w' , 32760);
l_file_seq := l_file_seq + 1;
SELECT value into l_nls_format
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT';
execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';

dbms_sql.parse( l_theCursor, l_query, dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );

for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
l_separator := ',';
end loop;

utl_file.new_line( l_output );

l_status := dbms_sql.execute(l_theCursor);

while (dbms_sql.fetch_rows(l_theCursor) > 0 ) loop

if l_rows_written >= 10000 then
utl_file.fclose( l_output );
l_output := utl_file.fopen( p_dir, 'test_'||l_file_seq||'_'||to_char(systimestamp, 'YYYYMMDDHH24MISSFF3'), 'w' , 32760);
for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
l_separator := ',';
end loop;
utl_file.new_line( l_output );

l_file_seq := l_file_seq + 1;
l_rows_written := 0;
end if;

l_separator := '';

for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i, l_columnValue );
utl_file.put( l_output, l_separator || l_columnValue );
l_separator := ',';
end loop;

utl_file.new_line(l_output );
l_rows_written := l_rows_written + 1;
end loop;

dbms_sql.close_cursor(l_theCursor);
utl_file.fclose(l_output );

execute immediate 'alter session set nls_date_format='''||l_nls_format||''' ';
exception
when others then
utl_file.fclose( l_output );
execute immediate 'alter session set nls_date_format='''||l_nls_format||''' ';
raise;
end;


It might be a good idea to offload the code for preparing CSV to a separate (sub)procedure to avoid code duplication. Also beware that original procedure has a nasty side effect of overriding your nls_date_format to dd-MON-yy without any regards to how it was set before execution; I fixed that.






share|improve this answer












Well, it's a fairly straightforward task. All you need to do is close the old file and open a new one. There's no need for outer loop, all you need is to put a condition and retrace the steps for preparing the csv.



create or replace procedure dump_table_to_csv
( p_tname in varchar2
, p_dir in varchar2
)
as
l_output utl_file.file_type;
l_theCursor integer default dbms_sql.open_cursor;
l_columnValue varchar2(4000);
l_status integer;
l_query varchar2(1000)
default 'select * from ' || p_tname;
l_colCnt number := 0;
l_separator varchar2(1);
l_descTbl dbms_sql.desc_tab;
l_file_seq number := 1;
l_rows_written number := 0;
l_nls_format varchar2(200);
begin
l_output := utl_file.fopen( p_dir, 'test_'||l_file_seq||'_'||to_char(systimestamp, 'YYYYMMDDHH24MISSFF3'), 'w' , 32760);
l_file_seq := l_file_seq + 1;
SELECT value into l_nls_format
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT';
execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';

dbms_sql.parse( l_theCursor, l_query, dbms_sql.native );
dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );

for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
l_separator := ',';
end loop;

utl_file.new_line( l_output );

l_status := dbms_sql.execute(l_theCursor);

while (dbms_sql.fetch_rows(l_theCursor) > 0 ) loop

if l_rows_written >= 10000 then
utl_file.fclose( l_output );
l_output := utl_file.fopen( p_dir, 'test_'||l_file_seq||'_'||to_char(systimestamp, 'YYYYMMDDHH24MISSFF3'), 'w' , 32760);
for i in 1 .. l_colCnt loop
utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
l_separator := ',';
end loop;
utl_file.new_line( l_output );

l_file_seq := l_file_seq + 1;
l_rows_written := 0;
end if;

l_separator := '';

for i in 1 .. l_colCnt loop
dbms_sql.column_value( l_theCursor, i, l_columnValue );
utl_file.put( l_output, l_separator || l_columnValue );
l_separator := ',';
end loop;

utl_file.new_line(l_output );
l_rows_written := l_rows_written + 1;
end loop;

dbms_sql.close_cursor(l_theCursor);
utl_file.fclose(l_output );

execute immediate 'alter session set nls_date_format='''||l_nls_format||''' ';
exception
when others then
utl_file.fclose( l_output );
execute immediate 'alter session set nls_date_format='''||l_nls_format||''' ';
raise;
end;


It might be a good idea to offload the code for preparing CSV to a separate (sub)procedure to avoid code duplication. Also beware that original procedure has a nasty side effect of overriding your nls_date_format to dd-MON-yy without any regards to how it was set before execution; I fixed that.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 15:03









Timekiller

1,80111112




1,80111112












  • thanks for the answer. The logic i applied is in below answer.
    – Laxmikant
    Nov 13 at 17:49


















  • thanks for the answer. The logic i applied is in below answer.
    – Laxmikant
    Nov 13 at 17:49
















thanks for the answer. The logic i applied is in below answer.
– Laxmikant
Nov 13 at 17:49




thanks for the answer. The logic i applied is in below answer.
– Laxmikant
Nov 13 at 17:49


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243090%2fhow-to-specify-max-number-of-rows-to-be-written-in-one-csv-file-in-oracle%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini