Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Problem creating text file from the contents of a table

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

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

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

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to recover a database from a user managed backup
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

580 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