sql loader [message #592840] |
Mon, 12 August 2013 12:09  |
 |
DIPRINKS
Messages: 57 Registered: July 2013 Location: Michigan
|
Member |
|
|
I have a scenario where the data file has values separated by ^.While loading this data into the table using sql loader I want to convert it into multiple lines.eg:
data file:
1|1013 park ridge~12345~irving|
2|2013 park ridge~12345~irving|
3|1013 park ridge|
while loading it into table i want the data like
ID ADDRESS
1 "1013 park ridge
12345
irving"
2 "2013 park ridge
12345
irving"
.....
My control file says:
load data
infile "/usr2/home2/adistest/h91ftp/temp/owb_test/owb_test1.csv"
preserve blanks INTO TABLE owbrep.owb_test1
TRUNCATE
fields terminated by '|' TRAILING NULLCOLS
(
id,
address
)
Please suggest what should I do?
[Updated on: Mon, 12 August 2013 12:10] Report message to a moderator
|
|
|
|
Re: sql loader [message #592844 is a reply to message #592841] |
Mon, 12 August 2013 12:36   |
 |
Barbara Boehmer
Messages: 9104 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
I posted the following on the OTN forums:
https://forums.oracle.com/thread/2568886
You can use the REPLACE function in your control file to replace whatever character, like ~ in your sample data, is where the newline should be with whatever your newline is on your system, like chr(10) as in the example below.
SCOTT@orcl12c_11gR2> host type owb_test1.csv
1|1013 park ridge~12345~irving|
2|2013 park ridge~12345~irving|
3|1013 park ridge|
SCOTT@orcl12c_11gR2> host type test.ctl
load data
infile "owb_test1.csv"
preserve blanks INTO TABLE owb_test1
TRUNCATE
fields terminated by '|' TRAILING NULLCOLS
(
id,
address "REPLACE (:address, '~', CHR(10))"
)
SCOTT@orcl12c_11gR2> create table owb_test1
2 (id number,
3 address varchar2(60))
4 /
Table created.
SCOTT@orcl12c_11gR2> host sqlldr scott/tiger control=test.ctl log=test.log
SQL*Loader: Release 12.1.0.1.0 - Production on Mon Aug 12 10:38:10 2013
Copyright (c) 1982, 2013, Oracle and/or its affiliates. All rights reserved.
Path used: Conventional
Commit point reached - logical record count 3
Table OWB_TEST1:
3 Rows successfully loaded.
Check the log file:
test.log
for more information about the load.
SCOTT@orcl12c_11gR2> select * from owb_test1
2 /
ID ADDRESS
---------- ------------------------------------------------------------
1 1013 park ridge
12345
irving
2 2013 park ridge
12345
irving
3 1013 park ridge
3 rows selected.
[Updated on: Mon, 12 August 2013 12:43] Report message to a moderator
|
|
|
|
|
|
Re: sql loader [message #592882 is a reply to message #592881] |
Mon, 12 August 2013 17:20   |
 |
DIPRINKS
Messages: 57 Registered: July 2013 Location: Michigan
|
Member |
|
|
Sorry.I pasted but it didnt get submitted earlier.
CREATE OR REPLACE PROCEDURE owb_prc_file_mult_column_gen(
p_file_dir VARCHAR2, -- mandatory (Oracle directory name)
p_file_name VARCHAR2, -- mandatory
p_sql_query VARCHAR2, -- Multiple column SQL SELECT statement that needs to be executed and processed
p_delimiter CHAR -- column delimiter
)
AS
l_cursor_handle INTEGER;
l_dummy NUMBER;
l_col_cnt INTEGER;
l_rec_tab DBMS_SQL.DESC_TAB;
l_current_col NUMBER(16);
l_current_line VARCHAR2(2047);
l_column_value VARCHAR2(32767);
l_file_handle UTL_FILE.FILE_TYPE;
l_print_text VARCHAR2(32767);
l_record_count NUMBER(16) := 0;
BEGIN
/* Open file for append*/
l_file_handle := UTL_FILE.FOPEN(p_file_dir, p_file_name, 'w', 32767); --Append Mode, 2047 chars per line max, possibly increasable
l_cursor_handle := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(l_cursor_handle, p_sql_query, DBMS_SQL.native);
l_dummy := DBMS_SQL.EXECUTE(l_cursor_handle);
/* Output column names and define them for latter retrieval of data */
DBMS_SQL.DESCRIBE_COLUMNS(l_cursor_handle, l_col_cnt, l_rec_tab); -- get column names
/* Append to file column headers */
l_current_col := l_rec_tab.FIRST;
IF (l_current_col IS NOT NULL) THEN
LOOP
DBMS_SQL.DEFINE_COLUMN(l_cursor_handle, l_current_col, l_column_value, 32767);
l_print_text := l_rec_tab(l_current_col).col_name || p_delimiter;
UTL_FILE.PUT (l_file_handle, l_print_text);
l_current_col := l_rec_tab.NEXT(l_current_col);
EXIT WHEN (l_current_col IS NULL);
END LOOP;
END IF;
UTL_FILE.PUT_LINE (l_file_handle,' ');
/* Append data for each row */
LOOP
EXIT WHEN DBMS_SQL.FETCH_ROWS(l_cursor_handle) = 0; -- no more rows to be fetched
l_current_line := '';
/* Append data for each column */
FOR l_current_col IN 1..l_col_cnt LOOP
DBMS_SQL.COLUMN_VALUE (l_cursor_handle, l_current_col, l_column_value);
l_print_text := l_column_value || p_delimiter;
l_current_line := l_current_line || replace(l_column_value,chr(13)||chr(10),'^') || p_delimiter;
END LOOP;
l_record_count := l_record_count + 1;
UTL_FILE.PUT_LINE (l_file_handle, l_current_line);
END LOOP;
UTL_FILE.FCLOSE (l_file_handle);
DBMS_SQL.CLOSE_CURSOR(l_cursor_handle);
EXCEPTION
WHEN OTHERS THEN
-- Release resources
IF DBMS_SQL.IS_OPEN(l_cursor_handle) THEN
DBMS_SQL.CLOSE_CURSOR(l_cursor_handle);
END IF;
IF UTL_FILE.IS_OPEN (l_file_handle) THEN
UTL_FILE.FCLOSE (l_file_handle);
END IF;
--RAISE ;
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.format_error_stack);
END owb_prc_file_mult_column_gen;
*BlackSwan added {code} tags. Please do so yourself in the future.
[Updated on: Mon, 12 August 2013 17:34] by Moderator Report message to a moderator
|
|
|
|
|
|
|