Home » SQL & PL/SQL » SQL & PL/SQL » utl_file (oracle 11 g)
utl_file [message #592612] |
Fri, 09 August 2013 18:48  |
 |
DIPRINKS
Messages: 57 Registered: July 2013 Location: Michigan
|
Member |
|
|
Hi,
I have a question.
The problem is I have newline characters in my varchar column.And when I write them in .csv file,it doesn't come in single cell .Instead it splits into multiple lines where ever it finds new line character.How should I resolve this?I want the result in single cell.Please help.
|
|
|
|
Re: utl_file [message #592621 is a reply to message #592614] |
Fri, 09 August 2013 23:12   |
 |
DIPRINKS
Messages: 57 Registered: July 2013 Location: Michigan
|
Member |
|
|
How can I do that in my utl_file procedure before I write my data to the csv file?
CREATE OR REPLACE PROCEDURE owb_prc_file_mult_column_test(
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;
dbms_output.put_line(l_print_text);
--UTL_FILE.PUT (l_file_handle, l_print_text);
UTL_FILE.new_line (l_file_handle, l_print_text);
l_current_col := l_rec_tab.NEXT(l_current_col);
dbms_output.put_line(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;
dbms_output.put_line(l_print_text);
l_current_line := l_current_line || l_column_value || p_delimiter;
dbms_output.put_line(l_current_line);
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;
*BlackSwan added {code} tags. Please do so yourself in the future.
[Updated on: Fri, 09 August 2013 23:21] by Moderator Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: utl_file [message #592877 is a reply to message #592621] |
Mon, 12 August 2013 16:29   |
 |
DIPRINKS
Messages: 57 Registered: July 2013 Location: Michigan
|
Member |
|
|
Ok.So the problem is resolved.But when I generate a csv file using the below code..my dates format gets changed.It displays wrong date..eg. 1/1/1855 changes to '01-JAN-55'.And when I load this csv agn using sql loader ,it gets loaded as '1/1/1955'.Please help.
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;
[Updated on: Mon, 12 August 2013 16:30] Report message to a moderator
|
|
|
|
|
|
|
|
|
Re: utl_file [message #593079 is a reply to message #593000] |
Tue, 13 August 2013 22:38   |
 |
DIPRINKS
Messages: 57 Registered: July 2013 Location: Michigan
|
Member |
|
|
Ok.So now that problem is resolved..I have another thing coming up..
My control file is:
OPTIONS (SKIP=1) load data
infile "/mnt/oraclefra/adis_etl_dir/ALLOCATION.csv"
INSERT preserve blanks INTO TABLE owbrep.ALLOCATION
TRUNCATE
FIELDS TERMINATED BY '~'
TRAILING NULLCOLS
(
ALLOCATION_CODE,
STATUS_CODE,
ACCOUNT,
SHORT_NAME,
LONG_NAME,
LONG_NAME2,
CFAE_PURPOSE_CODE,
CFAE_SUB_CODE,
RESTRICT_CODE,
FUND_NAME,
OWNERS,
PROGRAM_CODE,
ACCOUNT_2,
ATHLETICS_IND,
ALLOC_DEPT_CODE,
DEPARTMENT_NOTIFY,
ANNUAL_SW,
ALLOC_SCHOOL,
ALLOC_SUB_DEPT,
ALPHA_SORT,
GOAL_AMOUNT,
START_DATE,
STOP_DATE,
FISCAL_YEAR_TYPE,
CAMPUS,
AGENCY,
ALLOC_DIVISION,
ALLOC_PURPOSE,
ALLOC_FEE_IND,
ALLOC_NAMING_CODE,
ADMINISTRATOR_ID_NUMBER,
STEWARD_ID_NUMBER,
ENDOW_POOL_CODE,
ENDOW_TYPE_CODE,
MIN_AMT,
MIN_ACHIEVED_DATE,
DIST_START_DATE,
STEWARD_REPORTING_CODE,
XREF,
description "REPLACE (:description, '^', CHR(10))",
xcomment "REPLACE (:xcomment, '^', CHR(10))",
EXTRAMURAL_ATHLETICS,
ACCOUNTING_SCHOOL,
ACCOUNTING_DEPT,
GIFT_GRANT_TYPE,
CATEGORY1_CODE,
CATEGORY2_CODE,
CATEGORY3_CODE,
ALLOC_FEE_MIN_AMT,
ALLOC_FEE_ALT_CODE,
USE_FOR_AWC_IND,
date_added "to_date(:date_added,'MM/DD/YYYY HH:MI:SS AM')",
date_modified "to_date(:date_modified,'MM/DD/YYYY HH:MI:SS AM')",
OPERATOR_NAME,
USER_GROUP,
TRANSFER_USER_GROUP,
LOCATION_ID
)
And my log file says:
Record 1: Rejected - Error on table OWBREP.ALLOCATION, column LONG_NAME2.
ORA-00936: missing expression
Record 2: Rejected - Error on table OWBREP.ALLOCATION, column LONG_NAME2.
ORA-00936: missing expression
Please sugest what needs to be done.
|
|
|
|
Re: utl_file [message #593129 is a reply to message #593080] |
Wed, 14 August 2013 07:54   |
 |
DIPRINKS
Messages: 57 Registered: July 2013 Location: Michigan
|
Member |
|
|
-- Create table
create table ADVANCE.ALLOCATION
(
allocation_code VARCHAR2(16 BYTE) default ' ' not null,
status_code CHAR(1 BYTE) default ' ' not null,
account VARCHAR2(30 BYTE) default ' ' not null,
short_name VARCHAR2(40 BYTE) default ' ' not null,
long_name VARCHAR2(255 BYTE) default ' ' not null,
long_name2 VARCHAR2(255 BYTE) default ' ' not null,
cfae_purpose_code VARCHAR2(2 BYTE) default ' ' not null,
cfae_sub_code VARCHAR2(2 BYTE) default ' ' not null,
restrict_code VARCHAR2(3 BYTE) default ' ' not null,
fund_name VARCHAR2(10 BYTE) default ' ' not null,
owners VARCHAR2(10 BYTE) default ' ' not null,
program_code VARCHAR2(3 BYTE) default ' ' not null,
account_2 VARCHAR2(30 BYTE) default ' ' not null,
athletics_ind CHAR(1 BYTE) default ' ' not null,
alloc_dept_code VARCHAR2(7 BYTE) default ' ' not null,
department_notify CHAR(1 BYTE) default ' ' not null,
annual_sw CHAR(1 BYTE) default ' ' not null,
alloc_school VARCHAR2(4 BYTE) default ' ' not null,
alloc_sub_dept VARCHAR2(7 BYTE) default ' ' not null,
alpha_sort VARCHAR2(120 BYTE) default ' ' not null,
goal_amount NUMBER(14,2) default 0 not null,
start_date DATE,
stop_date DATE,
fiscal_year_type CHAR(1 BYTE) default ' ' not null,
campus VARCHAR2(3 BYTE) default ' ' not null,
agency VARCHAR2(3 BYTE) default ' ' not null,
alloc_division VARCHAR2(7 BYTE) default ' ' not null,
alloc_purpose VARCHAR2(5 BYTE) default ' ' not null,
alloc_fee_ind CHAR(1 BYTE) default ' ' not null,
alloc_naming_code VARCHAR2(3 BYTE) default ' ' not null,
administrator_id_number VARCHAR2(10 BYTE) default ' ' not null,
steward_id_number VARCHAR2(10 BYTE) default ' ' not null,
endow_pool_code VARCHAR2(3 BYTE) default ' ' not null,
endow_type_code VARCHAR2(3 BYTE) default ' ' not null,
min_amt NUMBER(14,2) default 0 not null,
min_achieved_date DATE,
dist_start_date DATE,
steward_reporting_code VARCHAR2(3 BYTE) default ' ' not null,
xref VARCHAR2(20 BYTE) default ' ' not null,
description VARCHAR2(255 BYTE) default ' ' not null,
xcomment VARCHAR2(255 BYTE) default ' ' not null,
extramural_athletics CHAR(1 BYTE),
accounting_school VARCHAR2(4 BYTE) default ' ' not null,
accounting_dept VARCHAR2(7 BYTE) default ' ' not null,
gift_grant_type VARCHAR2(3 BYTE) default ' ' not null,
category1_code VARCHAR2(3 BYTE) default ' ' not null,
category2_code VARCHAR2(3 BYTE) default ' ' not null,
category3_code VARCHAR2(3 BYTE) default ' ' not null,
alloc_fee_min_amt NUMBER(14,2) default 0 not null,
alloc_fee_alt_code CHAR(1 BYTE) default ' ' not null,
use_for_awc_ind CHAR(1 BYTE) default ' ' not null,
date_added DATE not null,
date_modified DATE default SYSDATE not null,
operator_name VARCHAR2(32 BYTE) default ' ' not null,
user_group VARCHAR2(2 BYTE) default ' ' not null,
transfer_user_group VARCHAR2(2 BYTE) default ' ' not null,
location_id NUMBER
)
tablespace ADVANCE_DATA
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Create/Recreate indexes
create unique index ADVANCE.ALLOCATION_KEY0 on ADVANCE.ALLOCATION (ALLOCATION_CODE, LOCATION_ID)
tablespace ADVANCE_DATA
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Grant/Revoke object privileges
grant select on ADVANCE.ALLOCATION to OWBREP;
|
|
|
|
|
|
|
Re: utl_file [message #593149 is a reply to message #593147] |
Wed, 14 August 2013 10:35  |
 |
DIPRINKS
Messages: 57 Registered: July 2013 Location: Michigan
|
Member |
|
|
There were plenty of problems with the control file.I had to apply to_date on the two fields START_DATE, STOP_DATE.The control file had "nvl(:LONG_NAME2, )" embedded in front of the field long_name2.So,I corrected this and everything is good.
|
|
|
Goto Forum:
Current Time: Mon Mar 03 21:37:13 CST 2025
|