PL/SQL programs can read and write operating system text files
http://download-west.oracl
Or
Use External Table as follws
Main Topics
Browse All TopicsHi All,
I want to be able to skip the first line that has header column titles and only start reading the data in the second line and onwards. How can I do this can someone please help me. There must be a generic way.
Please help.
darrylf290567
The example is:
first line of .csv file
SITE_ACCESS_CODE, MY_CODE, SITE_ID
second line of .csv file where I want to actually read the below data.
456T,ABC,123A
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
PL/SQL programs can read and write operating system text files
http://download-west.oracl
Or
Use External Table as follws
How you do it will depend on how you are reading the csv file.
1-
If you're using pl/sql, can I assume you're using utl_file?
If so, simply include a get_line as the first line of your procedure and simply ignore the results.
Then enter your loop to process the rest of the file.
2-
If you are using dbms_lob with bfiles, then use dbms_lob.instr to find the first end-of-line delimiter.
might be carrriage return, might be new line, might be both. Depends on your format.
Then start looping through your lines starting at that lob index +1.
3-
If you are using an external table and it has the headers in csv format too, that's easiest of all.
Simply exclude the first row in your cursor query.
select * from (select rownum rn, external_table.* from external_table)
where rn > 1
or, like the utl_file solution, fetch once, then ignore the results before entering the loop to process the rest of the cursor.
Hi All,
ststuber, Can you actually show me in code how i would do the reading of the example data i have given you in this question using PLSQL code using the UTL_FILE package?
Please can you help by actually doing the code for this example data and I will take it from there.
How do you ignore the first line using plsql, and then continue reading the second line, using the example data in the question.
Kindest Regards
darrylf290567
<<I want to be able to skip the first line that has header column....................
Following link will guide you with PL/SQL code
http://www.oracle-base.com
Hi sdstuber,
How do you do this in the PLSQL code:
1-
If you're using pl/sql, can I assume you're using utl_file?
If so, simply include a get_line as the first line of your procedure and simply ignore the results.
Then enter your loop to process the rest of the file.
Would you put get_line inside the loop or just before the loop starts?
How would getline inside the loop know NOT to read the first line? Will there be some sort of flag?
PLEASE HELP
darrylf290567
Hi sdstuber,
here is the file:
SITE_ASSET_ID,MONP_CODE,FT
100448,1,MTR,CONN,20070728
100448,1,MTR,CONN,20070729
100448,1,MTR,CONN,20070730
100448,1,MTR,CONN,20070731
100448,1,MTR,CONN,20070801
100448,1,MTR,CONN,20070802
100448,1,MTR,CONN,20070803
100448,1,MTR,CONN,20070804
100448,1,MTR,CONN,20070805
100448,1,MTR,CONN,20070806
100448,1,MTR,CONN,20070807
100448,1,MTR,CONN,20070808
100448,1,MTR,CONN,20070809
100448,1,MTR,CONN,20070810
100448,1,MTR,CONN,20070811
100448,1,MTR,CONN,20070813
100448,1,MTR,CONN,20070814
100448,1,MTR,CONN,20070815
100448,1,MTR,CONN,20070816
100448,1,MTR,CONN,20070817
100448,1,MTR,CONN,20070818
100448,1,MTR,CONN,20070820
100448,1,MTR,CONN,20070821
100448,1,MTR,CONN,20070822
100448,1,MTR,CONN,20070823
100448,1,MTR,CONN,20070824
100448,1,MTR,CONN,20070825
100448,1,MTR,CONN,20070826
100448,1,MTR,CONN,20070827
100448,1,MTR,CONN,20070828
100448,1,MTR,CONN,20070829
100448,1,MTR,CONN,20070830
100448,1,MTR,CONN,20070831
100448,1,MTR,CONN,20070901
100448,1,MTR,CONN,20070902
100448,1,MTR,CONN,20070903
100448,1,MTR,CONN,20070904
100448,1,MTR,CONN,20070905
100448,1,MTR,CONN,20070906
100448,1,MTR,CONN,20070907
100448,1,MTR,CONN,20070908
100448,1,MTR,CONN,20070909
100448,1,MTR,CONN,20070912
100448,1,MTR,CONN,20070913
100448,1,MTR,CONN,20070914
100448,1,MTR,CONN,20070915
100448,1,MTR,CONN,20070916
100448,1,MTR,CONN,20070917
100448,1,MTR,CONN,20070918
100448,1,MTR,CONN,20070919
kindest regards
darrylf290567
CREATE OR REPLACE DIRECTORY CSV_IMPORT_DIR AS 'your_directory_path_here'
GRANT READ, WRITE ON DIRECTORY CSV_IMPORT_DIR TO 'your_schema_here';
CREATE OR REPLACE PROCEDURE read_csv(p_filename IN VARCHAR2)
IS
v_filehandle UTL_FILE.file_type;
v_text VARCHAR2(4000);
v_eof BOOLEAN := FALSE;
v_temp VARCHAR2(100);
v_site_asset_id NUMBER;
v_monp_code NUMBER;
v_ftyp_code VARCHAR2(3);
v_mea_code VARCHAR2(4);
v_rea_dt_primary DATE;
v_rea_value NUMBER;
v_rea_origin VARCHAR2(4);
v_qi VARCHAR2(1);
BEGIN
v_filehandle := UTL_FILE.fopen('CSV_IMPORT
BEGIN
-- read header line
-- do nothing with the text
UTL_FILE.get_line(v_fileha
EXCEPTION
WHEN NO_DATA_FOUND
THEN
v_eof := TRUE;
END;
WHILE NOT v_eof
LOOP
BEGIN
UTL_FILE.get_line(v_fileha
v_text := TRIM(v_text);
IF v_text IS NOT NULL
THEN
-- Extract 1st element
v_temp := SUBSTR(v_text, 1, INSTR(v_text, ',', 1, 1) - 1);
v_site_asset_id := TO_NUMBER(v_temp);
-- Extract 2nd element
v_temp :=
SUBSTR(v_text,
INSTR(v_text, ',', 1, 1) + 1,
INSTR(v_text, ',', 1, 2) - 1 - INSTR(v_text, ',', 1, 1)
);
v_monp_code := TO_NUMBER(v_temp);
-- Extract 3rd element
v_temp :=
SUBSTR(v_text,
INSTR(v_text, ',', 1, 2) + 1,
INSTR(v_text, ',', 1, 3) - 1 - INSTR(v_text, ',', 1, 2)
);
v_ftyp_code := v_temp;
-- Extract 4th element
v_temp :=
SUBSTR(v_text,
INSTR(v_text, ',', 1, 3) + 1,
INSTR(v_text, ',', 1, 4) - 1 - INSTR(v_text, ',', 1, 3)
);
v_mea_code := v_temp;
-- Extract 5th element
v_temp :=
SUBSTR(v_text,
INSTR(v_text, ',', 1, 4) + 1,
INSTR(v_text, ',', 1, 5) - 1 - INSTR(v_text, ',', 1, 4)
);
v_rea_dt_primary := TO_DATE(v_temp, 'yyyymmddhh24miss');
-- Extract 6th element
v_temp :=
SUBSTR(v_text,
INSTR(v_text, ',', 1, 5) + 1,
INSTR(v_text, ',', 1, 6) - 1 - INSTR(v_text, ',', 1, 5)
);
v_rea_value := TO_NUMBER(v_temp);
-- Extract 7th element
v_temp :=
SUBSTR(v_text,
INSTR(v_text, ',', 1, 6) + 1,
INSTR(v_text, ',', 1, 7) - 1 - INSTR(v_text, ',', 1, 6)
);
v_rea_origin := v_temp;
-- Extract last element
v_temp := SUBSTR(v_text, INSTR(v_text, ',', 1, 7) + 1);
v_qi := v_temp;
-- Do something with the data...
DBMS_OUTPUT.put_line('v_si
DBMS_OUTPUT.put_line('v_mo
DBMS_OUTPUT.put_line('v_ft
DBMS_OUTPUT.put_line('v_me
DBMS_OUTPUT.put_line('v_re
DBMS_OUTPUT.put_line('v_re
DBMS_OUTPUT.put_line('v_re
DBMS_OUTPUT.put_line('v_qi
DBMS_OUTPUT.put_line('----
END IF;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
v_eof := TRUE;
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line('****
DBMS_OUTPUT.put_line(SQLER
DBMS_OUTPUT.put_line(v_tex
DBMS_OUTPUT.put_line(v_tem
DBMS_OUTPUT.put_line('****
END;
END LOOP;
UTL_FILE.fclose(v_filehand
RETURN;
EXCEPTION
WHEN OTHERS
THEN
IF UTL_FILE.is_open(v_filehan
THEN
UTL_FILE.fclose(v_filehand
END IF;
RAISE;
END read_csv;
The code I sent above doesn't consider fixed length data. It adjusts for every element. When you extract the data you have to put it "somewhere" so your variables need to be large enough to handle the biggest possible field. I defined the v_xxxxxx variables to match the data I saw in your examples but for unknown data I would increase the varchar2 variables to 32767
Hi sdstuber,
What I mean by a generic csv reader is that when the PLSQL code is reading the data it might start of identifying that the first comma is at position 6 (meaning that the data is 5 characters in length before the comma) in the first line. Whereas another csv file will have the first comma on the first line positioned at position 9 (meaning that the data is 8 characters in length before the comma). Can the code in your answer that you have supplied to me handle these issues?
Can you please help sdstuber? I really appreciate this.
data file:
SITE_ASSET_ID,MONP_CODE,FT
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
101007,w4,STO,OPTIMUM,2007
data file 2:
SITE_ASSET_ID,MONP_CODE,FT
17101,2,PWR,PMRKW,20000718
17101,2,PWR,PMRKW,20000818
17101,2,PWR,PMRKW,20000918
17101,2,PWR,PMRKW,20001018
17101,2,PWR,PMRKW,20001101
17101,2,PWR,PMRKW,20001108
17101,2,PWR,PMRKW,20001114
17101,2,PWR,PMRKW,20001122
17101,2,PWR,PMRKW,20001129
17101,2,PWR,PMRKW,20001205
17101,2,PWR,PMRKW,20001213
data file 3:
Site,mp,fac,meas,Reading Date,TARCON,origin
9714,1,MTR,TARCON,20070701
9714,1,MTR,TARCON,20070702
9714,1,MTR,TARCON,20070703
9714,1,MTR,TARCON,20070704
9714,1,MTR,TARCON,20070705
9714,1,MTR,TARCON,20070706
9714,1,MTR,TARCON,20070707
9714,1,MTR,TARCON,20070708
9714,1,MTR,TARCON,20070709
9714,1,MTR,TARCON,20070710
9714,1,MTR,TARCON,20070711
Kindest Regards
darrylf290567
You can't do dynamic variable declarations, so you can't create a 5-char variable for one file and 9-char variable for another file.
You can create a 32767-char variable and then read whatever you want.
The code I sent doesn't care how long the data is. Change all my variables to 32767 and it'll work for any file (up to 32767, of course)
If, you're looking for something that will enforce size rules on an entire file based on the contents of the first line of that file, that's something different. It's possible, post a new question with all of the rules you're looking for. I'll try to get to it sometime tonight or early tomorrow morning.
Business Accounts
Answer for Membership
by: sujith80Posted on 2008-01-18 at 00:34:52ID: 20688765
What tool are you using to load the data?
If you are using sqlloader, you can use the key word SKIP, your control line will look like :
LOAD DATA (SKIP 1)
.
.