?
Solved

Problem creating text file from the contents of a table

Posted on 2006-11-08
3
Medium Priority
?
491 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 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Suggested Courses

765 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