I need to fetch all rows in a specific partition of a table and print the values to a file.
CREATE OR REPLACE PROCEDURE
WriteRecordToFile
(
mypartition IN VARCHAR2,
myfilename IN VARCHAR2,
mydirloc IN VARCHAR2
)
IS
out_file utl_file.file_type;
chunk_size BINARY_INTEGER := 32767;
BEGIN
out_file := utl_file.fopen (mydirloc, myfilename, 'w', chunk_size);
FOR rec IN (
SELECT name FROM my_table PARTITION mypartition
)
LOOP
utl_file.put(out_file, rec.name);
utl_file.new_line(out_file);
END LOOP;
utl_file.fclose (out_file);
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Error while writing file: '|| sqlerrm);
IF utl_file.is_open(out_file) THEN
utl_file.fclose(out_file);
END IF;
END;
/
However, the above stored procedure only works if there is no 'PARTITION mypartition' in the select statement. I tried to use
EXECUTE IMMEDIATE 'SELECT name FROM my_table PARTITION mypartition'
in above but it still doesn't work. What is the correct way to do query with partition in the stored procedure? I am using Oracle 19c.
CREATE TABLE
andINSERT
statements for your sample data; the complete error message; and the expected output for your sample data. "It still doesn't work" is not a constructive statement as it does not tell us "what" does not work or what the issue was with the thing that is not working.