Solved

Problem creating text file from the contents of a table

Posted on 2006-11-08
3
489 Views
Last Modified: 2008-01-09
My objective is fairly simple.  I have a big central db with information that periodically has to be distributed to various different subsidiary ones dotted around the country (NB not the same data, but the same structure - i.e. some records go to subsid A, some to B etc. from the same set of tables).  As network access between the various database servers is spotty, unavailable at times and pretty variable in quality my solution is to create flat text files with the relevant information for each subsidiary database, put those files into appropriately named directory, and then find some way of getting the subsid dbs to read and upload the data in the files into their tables.  These text files may be made available to the subsids via mapped network drives, or they may possibly emailed or even sent to the subsid on CD.  

However I'm just on stage one at the moment and I have virtually 0 experience of PL/SQL Procs (expect many more questions with lucrative points...) .  

So I'm trying to write a procedure that will  output the contents of the set of tables given certain data conditions into flat text files (with specific names).   I've been trying for some time to get this to work with utl_file (although someone stop me right here if there's an easier way to do this as it does seem to be somewhat labourious)

The procedure I've written so far with some assistance from previous answers is:
CREATE OR REPLACE PROCEDURE TEST1 AS
BEGIN
DECLARE
  fileHandler UTL_FILE.FILE_TYPE;
  CURSOR C1 IS SELECT * FROM <mytab> where <various conditions>;
BEGIN
  fileHandler := UTL_FILE.FOPEN('C:\export\utl_file', 'myfile.123', 'w');

FOR C1_REC IN C1 LOOP
  UTL_FILE.PUT_LINE(fileHandler, C1_REC);
  UTL_FILE.NEW_LINE(fileHandler,1);
 END LOOP;
  UTL_FILE.FCLOSE(fileHandler);
END;
END;

This produces the following errors
10/3     PLS-00306: wrong number or types of arguments in call to 'PUT_LINE'
10/3     PL/SQL: Statement ignored  

Now as far as I can see from the spec of utl_file, this has the right number of arguments, so it must be that it doesn't like the type of data, which is supposed to be a record from the table.  


i.e. let's say my select statement results in output that looks like:
06 TESTC 198700 090090 FRED
06 TESTC 198704 9869 JOE
etc.
this is exactly what the text file would look like.  

(Although eventually I suspect I may want the fields to be delimited by something like "|" but that's probably the subject of the next question unless you can add this in now, and it depends on how the get_line bit of utl_files works.)

Any ideas how I can achieve this?


0
Comment
Question by:dramacqueen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Expert Comment

by:Stephen Lappin
ID: 17896706
You cannot supply a record type as an argument to UTL_FILE.PUT_LINE. You can only use basic datatypes (NUMBER, VARCHAR2, DATE etc).
 Do something like this:

UTL_FILE.PUT(fileHandler, C1_REC.col1);
UTL_FILE.PUT(fileHandler, ' ');   --field seperator
UTL_FILE.PUT(fileHandler, C1_REC.col2);
UTL_FILE.PUT(fileHandler, ' ');   --field seperator
UTL_FILE.PUT(fileHandler, C1_REC.col3);
UTL_FILE.PUT(fileHandler, ' ');   --field seperator
UTL_FILE.PUT(fileHandler, C1_REC.col4);
UTL_FILE.PUT(fileHandler, ' ');   --field seperator
UTL_FILE.PUT_LINE(fileHandler, C1_REC.col5);
0
 
LVL 14

Accepted Solution

by:
GGuzdziol earned 500 total points
ID: 17896791
Here is ready routine to do dump table contents, it's makes it csv format - it's easy to override this.
http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:68212348056
http://asktom.oracle.com/~tkyte/flat/index.html
0
 
LVL 2

Author Comment

by:dramacqueen
ID: 17897330
Thanks Gguzdziol, this fits the bill and I can make it do what I want I think.

lappins, sorry no points this time as although you explain why it's not working your solution would be impossible as these tables have 40+ columns.  But thanks for explaining the reason for no compile.  Figured it was something like that....
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question