Solved

Problem creating text file from the contents of a table

Posted on 2006-11-08
3
485 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:lappins
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now