0

I'm trying to do something fairly simple, I am trying to automate the removal and back up of tables from my personal table space. I have about 100 tables and want to get rid of all of them (except a table that I'm using to store the table names), but want to keep the data from the tables in case I need them sometime in the future. Below is the code I am trying to use to accomplish this. I am getting an error on the ref cursor, which I'll include below my code. I half expect somebody to tell me I'm an idiot and explain an easier way to do this. If not, please tell me what I'm doing wrong with the way that I am doing it, thanks.

DECLARE
    v_folder_name        VARCHAR2(100) := 'MY_FOLDER';
    TYPE QRY_CURSOR      IS REF CURSOR;
    v_qry_cursor         QRY_CURSOR;
    v_file_name          VARCHAR2(320);
    v_file               sys.utl_file.file_type;
    v_max_buffer_length  CONSTANT BINARY_INTEGER := 32767;
    v_qry_str            VARCHAR2(4000); --I've tried this with 32767, made no difference
    v_drop_string        VARCHAR2(4000);
    v_dynamic_record     VARCHAR2(4000); --tried this with 32767 also

CURSOR GET_TABLE_NAMES IS
    SELECT * FROM TEMP_BACKUP_TABLE WHERE TABLE_NAME <> 'TEMP_BACKUP_TABLE';

FUNCTION startFile(file_name VARCHAR2)
    --working function, used with many procedures, left out for brevity
END startFile;

FUNCTION closeFile(file_name VARCHAR2)
    --working function, used with many procedures, left out for brevity
END closeFile;

BEGIN
    INSERT INTO TEMP_BACKUP_TABLE SELECT DISTINCT TABLE_NAME FROM ALL_TAB_COLS WHERE OWNER = 'ME';
    COMMIT;
FOR REC IN GET_TABLE_NAMES LOOP
    v_file_name := REC.TABLE_NAME;
    v_file := startFile(v_file_name);
    v_qry_str := 'SELECT * FROM ' || v_file_name;
    v_drop_string := 'DROP TABLE ' || v_file_name;
    OPEN v_qry_cursor FOR v_qry_str;  -- this is the line that returns an error
    LOOP
        FETCH v_qry_cursor INTO v_dynamic_record;
        EXIT WHEN v_qry_cursor%NOTFOUND;
        sys.utl_file.put_line(v_file, v_dynamic_record);
    END LOOP;

    CLOSE v_qry_cursor;
    EXECUTE IMMEDIATE v_drop_string;
    COMMIT;

    v_file := closeFile(v_file_name);
END LOOP;
DELETE FROM TEMP_BACKUP_TABLE;
END;

The error I'm getting is as follows:

Error report:
ORA-00932: inconsistent datatypes: expected - got -
ORA-06512: at line 73
00932. 00000 - "inconsistent datatypes: expected %s got %s"
*cause:
*action:

Thanks for any help.

2
  • 1
    At a minimum, utl_file.put_line does not take arbitrary record and you can't fetch an arbitrary list of columns into a varchar2. You could iterate over each column and construct a SQL statement that concatenates the values from each column into a single string (including doing things like doing a to_char on your date or timestamp columns to save them in a particular format). Rather than writing a bunch of code, though, it would seem to make more sense to just export the tables using the command line exp or expdp executables. Commented Jun 9, 2016 at 18:43
  • @JustinCave You know, I had it concatenating the columns into a string when I first wrote this (a long time ago) and for some reason changed it, wasn't even thinking. Now I'm getting a "missing expression" error, so must be a problem with the SQL now. I am sure I can figure it out now. If you put your comment in as an answer I'll accept it. Thanks. Commented Jun 9, 2016 at 19:18

1 Answer 1

1

At a minimum, utl_file.put_line does not take arbitrary record and you can't fetch an arbitrary list of columns into a varchar2.

You could iterate over each column and construct a SQL statement that concatenates the values from each column into a single string. That would include doing things like putting a to_char with an explicit format mask on your date or timestamp columns, adding a delimiter, escaping any delimiters that exist in your data, etc. This is generally a rather tedious and error-prone process. And then you'll need to write a SQL*Loader control file to load the data back in the future.

It sounds like you'd be better off exporting the table using the Oracle export utility. That's a command-line utility (exp or expdp depending on whether you want to use the classic version or the DataPump version) that lets you export the table definition and data to a file that you can load later using the Oracle import utility.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.