Home » SQL & PL/SQL » SQL & PL/SQL » Formatting output file (3 Merged) (Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production PL/SQL Release 11.2.0.2.0 - Production CORE 11.2.0.2.0 Production TNS for Linux: Version 11.2.0.2.0 - Production NL)
Formatting output file (3 Merged) [message #565781] Thu, 06 September 2012 11:36 Go to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member

CREATE OR REPLACE PROCEDURE A516907.proc_data_T_DTIA_TS_ADDR
is cursor CSR1 IS SELECT * FROM T_DTIA_TS_ADDR ;
v_file UTL_FILE.FILE_TYPE ;
BEGIN
v_file := UTL_FILE.FOPEN( 'DATA_PUMP_DIR' , 'T_DTIA_TS_ADDR.txt','w',32767);
FOR CS1 IN CSR1
LOOP
UTL_file.put_line(v_file,CS1.TS_ADDR_DTIA_ID || ' , ' || CS1.CB_TS_DTIS_ID || ' , ' || CS1.TS_ADDR_TY_CD || ' , ' || CS1.CITY_NM || ' , ' || CS1.STR_ADDR_LINE_3_TX || ' , ' || CS1.STR_ADDR_LINE_5_TX || ' , ' || CS1.STR_ADDR_LINE_2_TX || ' , ' || CS1.CERT_EXCLUDE_IN || ' , ' || CS1.ZIP_CD || ' , ' || CS1.STR_ADDR_LINE_6_TX || ' , ' || CS1.DELVRY_POINT_CD || ' , ' || CS1.STR_ADDR_LINE_1_TX || ' , ' || CS1.PHONE_NO || ' , ' || CS1.FOREIGN_IN || ' , ' || CS1.CERTIFIED_IN || ' , ' || CS1.STR_ADDR_LINE_4_TX || ' , ' || CS1.DART_NAME_LINES_CT || ' , ' || CS1.AUTH_ID || ' , ' || CS1.USER_ID || ' , ' || CS1.LOCK_ID || ' , ' || CS1.SYS_TM_STMP || ' , ' || CS1.STATE_CD
) ;
end loop;
UTL_FILE.FCLOSE(v_file);
EXCEPTION
when others
then
dbms_output.put_line(sqlerrm);
end;
/


The procedure above is dynamically generated. It's simple enough in that I am calling "the parent procedure" by passing the table name in. After this the procedure for creating the output data file for that table is generated and that's what we have above. I am doing this to load data into DB2.

What I need is to be able to generate the output such that if the column is of type varchar, char (anything non numeric) then the data should have " around it. If the data is of type number it should not have ".

I suppose I could use something as follows in lieu of the procedure above

Declare
cursor c1 is select column_name, data_type from user_tab_Cols where table_name = 'Table Name here';

-- I would like to know how to use the utl_file.put_line
-- where if the column data type is varchar 2
-- I could get ' ||"|| CS1.TS_ADDR_TY_CD||" || '
-- and if it's a number -- || CS1.CB_TS_DTIS_ID ||

Any ideas ?

Thank you
Hari

[EDITED by LF: fixed version information; was copy/paste of the whole "select * from v$version" command]

[Updated on: Fri, 07 September 2012 13:59] by Moderator

Report message to a moderator

Re: Conditional Formatting of output [message #565784 is a reply to message #565781] Thu, 06 September 2012 11:39 Go to previous messageGo to next message
Michel Cadot
Messages: 68758
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Check all_tab_columns.

Regards
Michel
Re: Conditional Formatting of output [message #565789 is a reply to message #565781] Thu, 06 September 2012 11:53 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
Michel

My question is more specifically about the formatting of output.

If I had a table like below

tab_addr
(adr_id number(19,0),
add_line1 varchar2(200));

The following is what I could do to get the cursor

Cursor cs1 is select column_name, data_type from user_tab_cols
where table_name = 'tab_addr';

However, what would I do in the utl_file.put_line so that I could get the following

UTL_file.put_line(v_file, cs1.adr_id ||','||'"'||cs1.add_line1||'"'||);

The rest would remain the same as in the procedure tagged above.



Re: Conditional Formatting of output [message #565790 is a reply to message #565789] Thu, 06 September 2012 11:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68758
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
With the data_type use CASE to put or not '"' around the value.

Keep your lines of code in 80 character width: no more than 80 characters on each line in your post.

Regards
Michel

[Updated on: Thu, 06 September 2012 11:57]

Report message to a moderator

Re: Conditional Formatting of output [message #565791 is a reply to message #565781] Thu, 06 September 2012 11:56 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
just use IF .... THEN construct to build the desired code/string

what about DATE datatypes?
Re: Conditional Formatting of output [message #565799 is a reply to message #565791] Thu, 06 September 2012 13:22 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
Blackswan,

Here is what I have so far

SELECT STRAGG(DATA_TYPE) FROM
(SELECT COLUMN_NAME,
(CASE DATA_TYPE
WHEN 'VARCHAR2' THEN '"''||'||COLUMN_NAME||'||''"'
WHEN 'NUMBER' THEN COLUMN_NAME
WHEN 'INTEGER' THEN COLUMN_NAME
WHEN 'CHAR' THEN '"'''||COLUMN_NAME||'''"'
WHEN 'TIMESTAMP' THEN (to_CHAR(COLUMN_NAME, 'YYYY-MM-DD')|| '-'||to_CHAR(COLUMN_NAME,'HH24'||'.'||'MI'||'.'||'SS'||'.'||'FF'))
END) DATA_TYPE FROM USER_TAB_COLS WHERE TABLE_NAME = 'T_DTIA_TS_ADDR')

Stragg is a string agregator function that I have used extensively over the years.
This is from Tom Kyte's site.


TS_ADDR_DTIA_ID,CB_TS_DTIS_ID,"'TS_ADDR_TY_CD'","'CITY_NM'","'STR_ADDR_LINE_3_TX'","'STR_ADDR_LINE_5_TX'","'STR_ADDR_LINE_2_TX' ","'CERT_EXCLUDE_IN'","'ZIP_CD'","'STR_ADDR_LINE_6_TX'","'DELVRY_POINT_CD'","'STR_ADDR_LINE_1_TX'","'PHONE_NO'","'FOREIGN_IN' ","'CERTIFIED_IN'","'STR_ADDR_LINE_4_TX'",DART_NAME_LINES_CT,"'AUTH_ID'","'USER_ID'",LOCK_ID,"'STATE_CD'"

I am still trying to get the correct format out.
The output is
(http://www.sqlsnippets.com/en/topic-11591.html
Re: Conditional Formatting of output [message #565800 is a reply to message #565799] Thu, 06 September 2012 13:22 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
Correction to my last post

The output is
TS_ADDR_DTIA_ID,CB_TS_DTIS_ID,"'TS_ADDR_TY_CD'","'CITY_NM'","'STR_ADDR_LINE_3_TX'","'STR_ADDR_LINE_5_TX'","'STR_ADDR_LINE_2_TX' ","'CERT_EXCLUDE_IN'","'ZIP_CD'","'STR_ADDR_LINE_6_TX'","'DELVRY_POINT_CD'","'STR_ADDR_LINE_1_TX'","'PHONE_NO'","'FOREIGN_IN' ","'CERTIFIED_IN'","'STR_ADDR_LINE_4_TX'",DART_NAME_LINES_CT,"'AUTH_ID'","'USER_ID'",LOCK_ID,"'STATE_CD'"

Re: Conditional Formatting of output [message #565801 is a reply to message #565800] Thu, 06 September 2012 13:23 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
http://www.sqlsnippets.com/en/topic-11591.html

The link above has details about the stragg function.
Re: Conditional Formatting of output [message #565802 is a reply to message #565801] Thu, 06 September 2012 13:29 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
The issue is that this procedure is generated dynamically. Which means that the columns will differ based on the table name that is passed in.
I think I might be able to get away from using a cursor and that would be a big help. If the format for utl_file.put_line can be a string value that is formed passed in, that would make life a whole lot easier.
Re: Conditional Formatting of output [message #565803 is a reply to message #565802] Thu, 06 September 2012 13:32 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
11:32:25 SQL> @x
11:32:27 SQL> declare
11:32:27   2  x varchar2(4000);
11:32:27   3  begin
11:32:27   4  x := 'String1 ';
11:32:27   5  x := x || 'String2 ';
11:32:27   6  x := x || 'String3 ';
11:32:27   7  x := x || 'String4 ';
11:32:27   8  x := x || 'String5 ';
11:32:27   9  x := x || 'String6 ';
11:32:27  10  dbms_output.put_line(x);
11:32:27  11  end;
11:32:27  12  /
String1 String2 String3 String4 String5 String6

PL/SQL procedure successfully completed.

11:32:27 SQL> 

Re: Conditional Formatting of output [message #565806 is a reply to message #565803] Thu, 06 September 2012 13:41 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
BlackSwan

The output should be "String1", "String2", "String3", "String4", "String5", "String6"

Re: Conditional Formatting of output [message #565807 is a reply to message #565806] Thu, 06 September 2012 13:43 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
The other thing is that the data has to be written from a table using utl_file.put_line.
Re: Conditional Formatting of output [message #565808 is a reply to message #565807] Thu, 06 September 2012 13:47 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>The other thing is that the data has to be written from a table
where the string originates does not matter

>using utl_file.put_line.
DBMS_OUTPUT.PUT_LINE operates the same as UTL_FILE.PUT_LINE.

just run a simple stand alone sanity test code to show proof of concept is valid.
Re: Conditional Formatting of output (2 Merged) [message #565812 is a reply to message #565781] Thu, 06 September 2012 15:13 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
SQL> declare
2 x varchar2(4000);
3 begin
4 x := 'String1 ';
5 x := x || ' "String2" ' ||',';
6 x := x || ' "String3" '||',';
7 x := x || ' "String4" '||',';
8 x := x || ' "String5" '||',';
9 x := x || ' "String6" ';
10 dbms_output.put_line(x);
11 end;
12 /
String1 "String2" , "String3" , "String4" , "String5" , "String6"

PL/SQL procedure successfully completed.

SQL>

The above output is what I would need.
Re: Conditional Formatting of output (2 Merged) [message #565814 is a reply to message #565812] Thu, 06 September 2012 15:51 Go to previous messageGo to next message
suryay22
Messages: 5
Registered: September 2012
Location: Bangalore
Junior Member
i want to know difference between sql *loader and utl_file??
Re: Conditional Formatting of output (2 Merged) [message #565815 is a reply to message #565814] Thu, 06 September 2012 17:00 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
suryay22 wrote on Thu, 06 September 2012 13:51
i want to know difference between sql *loader and utl_file??


when all else fails Read The Fine Manual

http://docs.oracle.com/cd/E11882_01/server.112/e22490/ldr_concepts.htm#i1005437

http://docs.oracle.com/cd/E11882_01/timesten.112/e21645/u_file.htm#TTPLP069
Formatting output file [message #565874 is a reply to message #565781] Fri, 07 September 2012 12:44 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
DECLARE
 v_file  UTL_FILE.FILE_TYPE ; 
 vstragg  varchar2(4000);
BEGIN
 v_file := UTL_FILE.FOPEN( 'DATA_PUMP_DIR' , 'T_DTIA_TS_ADDR.txt','w',32767);
select replace(stragg(DATA_TYPE), ',',' || '' , ''') INTO vstragg from (
SELECT COLUMN_NAME,
  (CASE DATA_TYPE
    WHEN 'VARCHAR2' THEN '"||'||COLUMN_NAME||'||"'
    WHEN 'NUMBER' THEN COLUMN_NAME
    WHEN 'CHAR' THEN  '"||'||COLUMN_NAME||'||"'
    WHEN substr('TIME', 1, 4) THEN  (to_CHAR(COLUMN_NAME, 'YYYY-MM-DD')|| '-'||
to_CHAR(COLUMN_NAME,'HH24'||'.'||'MI'||'.'||'SS'||'.'||'FF')) 
    END) DATA_TYPE   FROM USER_TAB_COLS WHERE TABLE_NAME = 'T_DTIA_TS_ADDR');

UTL_file.put_line( v_file,    vstragg);
UTL_FILE.FCLOSE(v_file); 
  EXCEPTION 
  when others 
  then 
 dbms_output.put_line(sqlerrm); 
  end;
/


The output of the file is

ortp01db01.fmr.com oracle> more T_DTIA_TS_ADDR.txt
TS_ADDR_DTIA_ID || ' , 'CB_TS_DTIS_ID || ' , '"||TS_ADDR_TY_CD||" || ' , '"||CITY_NM||" || ' , '"||STR_ADDR_LINE_3_TX||" || ' , '"||STR_ADDR_LINE_5_TX||" || '
, '"||STR_ADDR_LINE_2_TX||" || ' , '"||CERT_EXCLUDE_IN||" || ' , '"||ZIP_CD||" || ' , '"||STR_ADDR_LINE_6_TX||" || ' , '"||DELVRY_POINT_CD||" || ' , '"||STR_
ADDR_LINE_1_TX||" || ' , '"||PHONE_NO||" || ' , '"||FOREIGN_IN||" || ' , '"||CERTIFIED_IN||" || ' , '"||STR_ADDR_LINE_4_TX||" || ' , 'DART_NAME_LINES_CT || '
, '"||AUTH_ID||" || ' , '"||USER_ID||" || ' , 'LOCK_ID || ' , '"||STATE_CD||"


These are the column names and not the actual data.

Do I have to somehow take the value for vstragg
variable and convert it into a cursor ?

Please recommend/suggest

[Updated on: Fri, 07 September 2012 14:00] by Moderator

Report message to a moderator

Re: Formatting output file [message #565875 is a reply to message #565874] Fri, 07 September 2012 13:01 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
consider testing against a test table that contains 1 column of each supported datatype & post CREATE TABLE statement
consider testing against a test table that contains only 2 or 3 rows of test data & post INSERT statements.

what is expected & desired content from test data as it resides in output text file?
Re: Formatting output file [message #565877 is a reply to message #565874] Fri, 07 September 2012 13:07 Go to previous messageGo to next message
Michel Cadot
Messages: 68758
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Michel Cadot wrote on Thu, 06 September 2012 18:55
With the data_type use CASE to put or not '"' around the value.

Keep your lines of code in 80 character width: no more than 80 characters on each line in your post.

Regards
Michel


Re: Formatting output file [message #565885 is a reply to message #565875] Fri, 07 September 2012 13:57 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
CREATE TABLE OUTPUT_TEST
(
ID NUMBER(19),
VAR_REL VARCHAR2(200 BYTE),
DT DATE,
SYSTM TIMESTAMP(6)
)

Insert statements

SET DEFINE OFF;
Insert into OUTPUT_TEST
(ID, VAR_REL, DT, SYSTM)
Values
(1, 'This is a test', TO_DATE('09/07/2012 14:08:16', 'MM/DD/YYYY HH24:MI:SS'), TO_TIMESTAMP('09/07/2012 2:08:39.361678 PM','MM/DD/YYYY fmHH12fm:MI:SS.FF AM'));
Insert into OUTPUT_TEST
(ID, VAR_REL, DT, SYSTM)
Values
(2, 'Second row', TO_DATE('09/06/2012 14:09:04', 'MM/DD/YYYY HH24:MI:SS'), TO_TIMESTAMP('09/06/2012 2:09:10.000000 PM','MM/DD/YYYY fmHH12fm:MI:SS.FF AM'));
COMMIT;

Data in this table is as follows

ID VAR_REL DT SYSTM
1 This is a test 09/07/2012 14:08:16 09/07/2012 2:08:39.361678 PM
2 Second row 09/06/2012 14:09:04 09/06/2012 2:09:10.000000 PM


The output should be dumped in the following format to a file

1,"This is a test",2012-09-07 14:08:16,2012-09-07-14.08.39.361678
2,"Second row",2012-09-06 14:09:04,2012-09-06-14.09.10.000000

How would I accomplish this?

Thank you for your help.
Re: Formatting output file [message #565886 is a reply to message #565885] Fri, 07 September 2012 14:04 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
so why did your most code SELECT FROM USER_TAB_COLS and not FROM T_DTIA_TS_ADDR; where actual data resides?
the code needs to SELECT FROM OUTPUT_TEST .... if you want "This is a test" in the output file.
Re: Formatting output file [message #565887 is a reply to message #565886] Fri, 07 September 2012 14:15 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
I have to make this generic to where the table_name is going to be passed in.
The idea being that based on the data_type column from user_tab_cols I was thinking
that I could write the cursor that would format the columns and then somehow loop it in.

Had there been no case for formatting, I am able to select the columns from the table itself
and then simply use utl_file.put_line to write the file out.

The formatting is required because this data output will be used to feed DB2 tables.

Regards and Thanks
Hari
Re: Formatting output file [message #565888 is a reply to message #565887] Fri, 07 September 2012 14:17 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
Note : the reason that I say that I have to make it generic is that
the procedure/anonymous block will be dynamically generated.

I cannot have any of the columns hard coded. Based on the table name,
I have to get the column names from either user_tables or user_tab_cols.

I would think that for the formatting I will need the data types and that would
come only from the user_tab_cols.
Re: Formatting output file [message #565889 is a reply to message #565888] Fri, 07 September 2012 14:27 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I would think that for the formatting I will need the data types and that would come only from the user_tab_cols.
I agree that user_tab_columns must be queried; but so must the table that actually contains the data be queried.

I have a saying.
First make it work, then make it fancy.
If you can not get the desired results by hard coding initially; you'll never succeed doing it dynamically.
Re: Formatting output file [message #565890 is a reply to message #565889] Fri, 07 September 2012 14:31 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
This is what I have so far

DECLARE
v_file UTL_FILE.FILE_TYPE ;
vstragg varchar2(4000);
BEGIN
v_file := UTL_FILE.FOPEN( 'DATA_PUMP_DIR' , 'output_test.txt','w',32767);
select replace(stragg(DATA_TYPE), ',',' || '' , ''') into vstragg from (
SELECT COLUMN_NAME,
(CASE DATA_TYPE
WHEN 'VARCHAR2' THEN '"||'||COLUMN_NAME||'||"'
WHEN 'NUMBER' THEN COLUMN_NAME
WHEN 'CHAR' THEN '"||'||COLUMN_NAME||'||"'
WHEN 'DATE' THEN COLUMN_NAME
ELSE COLUMN_NAME
END) DATA_TYPE FROM USER_TAB_COLS WHERE TABLE_NAME = 'OUTPUT_TEST');

UTL_file.put_line( v_file, vstragg);
UTL_FILE.FCLOSE(v_file);
EXCEPTION
when others
then
dbms_output.put_line(sqlerrm);
end;
/

Output for output_test.txt file

more output_test.txt
ID || ' , '"||VAR_REL||" || ' , 'DT || ' , 'SYSTM


Re: Formatting output file [message #565891 is a reply to message #565890] Fri, 07 September 2012 14:34 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>more output_test.txt
>ID || ' , '"||VAR_REL||" || ' , 'DT || ' , 'SYSTM

above does not look like below

>The output should be dumped in the following format to a file
>1,"This is a test",2012-09-07 14:08:16,2012-09-07-14.08.39.361678
>2,"Second row",2012-09-06 14:09:04,2012-09-06-14.09.10.000000
Re: Formatting output file [message #565892 is a reply to message #565891] Fri, 07 September 2012 15:20 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
I know. I am trying to get it into that format.

The select statement that I have

select replace(stragg(DATA_TYPE), ',',' || '' , ''') into vstragg from (
SELECT COLUMN_NAME,
(CASE DATA_TYPE
WHEN 'VARCHAR2' THEN '"||'||COLUMN_NAME||'||"'
WHEN 'NUMBER' THEN COLUMN_NAME
WHEN 'CHAR' THEN '"||'||COLUMN_NAME||'||"'
WHEN 'DATE' THEN COLUMN_NAME
ELSE COLUMN_NAME
END) DATA_TYPE FROM USER_TAB_COLS WHERE TABLE_NAME = 'OUTPUT_TEST');

gives me the output above.

I need to use what's stored in the string as a ref cursor.
Re: Formatting output file [message #565893 is a reply to message #565892] Fri, 07 September 2012 15:29 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>I need to use what's stored in the string as a ref cursor.

content of string below does not look any like valid SQL syntax!

>ID || ' , '"||VAR_REL||" || ' , 'DT || ' , 'SYSTM


I have a saying.
First make it work, then make it fancy.
If you can not get the desired results by hard coding initially; you'll never succeed doing it dynamically.
Re: Formatting output file [message #565897 is a reply to message #565893] Fri, 07 September 2012 16:32 Go to previous messageGo to next message
suryay22
Messages: 5
Registered: September 2012
Location: Bangalore
Junior Member
when i am going to create spool file, I am getting error i.e

SQL> spool outfile.txt
SP2-0606: Cannot create SPOOL file "outfile.txt"

please answer me...
Re: Formatting output file [message #565898 is a reply to message #565897] Fri, 07 September 2012 16:36 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
Suryay22
Do you have write permissions where you are running your code. This might sound too obvious.
Re: Formatting output file [message #565899 is a reply to message #565898] Fri, 07 September 2012 16:43 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member

select replace(stragg(DATA_TYPE), ',',' || '' , ''') --into vstragg
from (
SELECT COLUMN_NAME,
(CASE DATA_TYPE
WHEN 'VARCHAR2' THEN '"||'||COLUMN_NAME||'||"'
WHEN 'NUMBER' THEN COLUMN_NAME
WHEN 'CHAR' THEN '"||'||COLUMN_NAME||'||"'
WHEN 'DATE' THEN COLUMN_NAME
ELSE COLUMN_NAME
END) DATA_TYPE FROM USER_TAB_COLS WHERE TABLE_NAME = 'OUTPUT_TEST');

-- Output

ID || ' , '"||VAR_REL||" || ' , 'DT || ' , 'SYSTM

Somehow, we need to get the output into a cursor so that the table data can be looped.
Re: Formatting output file [message #565901 is a reply to message #565899] Fri, 07 September 2012 20:27 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
I suggest that you use TO_CHAR to ensure the content of the text file contain properly formatted DATES & TIMESTAMPS
Re: Formatting output file [message #566032 is a reply to message #565901] Mon, 10 September 2012 11:37 Go to previous messageGo to next message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
CREATE OR REPLACE PROCEDURE run_query1(p_sql IN VARCHAR2
                                     ,p_dir IN VARCHAR2
                                     ,p_header_file IN VARCHAR2
                                     ,p_data_file IN VARCHAR2 := NULL) IS
  v_finaltxt  VARCHAR2(4000);
  v_v_val     VARCHAR2(4000);
  v_n_val     NUMBER;
  v_d_val     DATE;
  v_ret       NUMBER;
  c           NUMBER;
  d           NUMBER;
  col_cnt     INTEGER;
  f           BOOLEAN;
  rec_tab     DBMS_SQL.DESC_TAB;
  col_num     NUMBER;
  v_fh        UTL_FILE.FILE_TYPE;
  v_samefile  BOOLEAN := (NVL(p_data_file,p_header_file) = p_header_file);
BEGIN
  c := DBMS_SQL.OPEN_CURSOR;
  DBMS_SQL.PARSE(c, p_sql, DBMS_SQL.NATIVE);
  d := DBMS_SQL.EXECUTE(c);
  DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
  dbms_output.put_line (' col_cnt : ' || col_cnt);
  FOR j in 1..col_cnt
  LOOP
    CASE rec_tab(j).col_type
      WHEN 1 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
      WHEN 2 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_n_val);
      WHEN 12 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_d_val);
    ELSE
      DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
    END CASE;
  END LOOP;
  -- This part outputs the HEADER
 v_fh := UTL_FILE.FOPEN(upper(p_dir),p_header_file,'w',32767);

    DBMS_OUTPUT.PUT_LINE(v_finaltxt);
   UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
   IF NOT v_samefile THEN
     UTL_FILE.FCLOSE(v_fh);
  END IF;
  
  -- This part outputs the DATA
  IF NOT v_samefile THEN
    v_fh := UTL_FILE.FOPEN(upper(p_dir),p_data_file,'w',32767);
  END IF;
  LOOP
    v_ret := DBMS_SQL.FETCH_ROWS(c);
    EXIT WHEN v_ret = 0;
    v_finaltxt := NULL;
    FOR j in 1..col_cnt
    LOOP
      CASE rec_tab(j).col_type
        WHEN 1 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
                    v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
        WHEN 2 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
                    v_finaltxt := ltrim(v_finaltxt||','||v_n_val,',');
        WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
                 v_finaltxt := ltrim(v_finaltxt||','||to_char(v_d_val,'YYYY-MM-DD'||'-'||'HH24.MI.SS'));
              --   l,'DD/MM/YYYY HH24:MI:SS'),',');
      ELSE
        v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
      END CASE;
    END LOOP;
  --  DBMS_OUTPUT.PUT_LINE(v_finaltxt);
    UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
  END LOOP;
  UTL_FILE.FCLOSE(v_fh);
  DBMS_SQL.CLOSE_CURSOR(c);
END;
/


I found something online that was a good match.
Link :- https://forums.oracle.com/forums/thread.jspa?threadID=613034
I am trying out how to match this with what we need.

The output that I am getting is below

ortp01db01.fmr.com oracle> more OUTPUT_TEST.txt

1,"This is a test",2012-09-07-14.08.16,"This is a test"
2,"Second row",2012-09-06-14.09.04,"Second row"

It should be
1,"This is a test",2012-09-07 14:08:16,2012-09-07-14.08.39.361678
2,"Second row",2012-09-06 14:09:04,2012-09-06-14.09.10.000000

I am only concerned about the looping since the procedure does exactly
what I am looking for.
Re: Formatting output file [message #566306 is a reply to message #566032] Wed, 12 September 2012 09:08 Go to previous message
hbhanujan
Messages: 28
Registered: August 2012
Location: Dallas, TX
Junior Member
http://docs.oracle.com/cd/E11882_01/server.112/e17118/sql_elements001.htm

Gurus

Thank you for all your help. That was the issue. I was not aware that there were specific codes for dbms_sql.define_Columns.

Regards and thank you again.
Hari
Previous Topic: using between clause in numaric data and in multi tables
Next Topic: Materialized view redefinition
Goto Forum:
  


Current Time: Tue May 27 13:53:18 CDT 2025