Problem creating text file from the contents of a table
Posted on 2006-11-08
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
CURSOR C1 IS SELECT * FROM <mytab> where <various conditions>;
fileHandler := UTL_FILE.FOPEN('C:\export\utl_file', 'myfile.123', 'w');
FOR C1_REC IN C1 LOOP
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
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?