Oracle PL/SQL - How to escape colon (:), being misinterpreted for bind variable

前端 未结 2 1690
梦毁少年i
梦毁少年i 2021-01-20 00:02

I have a small PL/SQL script that I\'m using to try and copy data between two Oracle database instances.

I\'m calling the SQL script with (sanitised):



        
相关标签:
2条回答
  • 2021-01-20 00:49

    to try and copy data between two Oracle database instances.

    You're mixing up SQL*Plus commands with PL/SQL. But, there is no need to write code yourself for that. You can use Oracle Data Pump Export and Import. Use it with the CONTENT=DATA_ONLY option to mimic SQL*Plus' COPY command.

    Regards,
    Rob.

    0 讨论(0)
  • 2021-01-20 00:50

    You need to put quotes around the positional variable when you assign it, so the whole value is interpreted as a string at that point:

    destination_connstring VARCHAR(20) := '&6';
    

    I don't believe PL/SQL variable assignment supports escaping in the sense that LIKE does, and if it did you'd have to modify your inputs before you called the script which wouldn't be ideal.


    Moving away from your original question a bit...

    You'll also need to use some form of dynamic SQL to take action based on the passed parameters and cursor values; and COPY is an SQL*Plus command so you can't call it from PL/SQL anyway. I'd suggest you use the PL/SQL block to generate a separate SQL script containing all the commands, via spool and dbms_output, which you then execute after the block completes. Something like:

    SET SERVEROUTPUT ON SIZE 100000 FORMAT WRAPPED;
    SET TRIMOUT ON
    SET TRIMSPOOL ON
    SET VERIFY OFF
    SET LINES 1024
    
    SPOOL tmp_copy_commands.sql
    SET TERMOUT OFF
    SET FEEDBACK OFF
    
    DECLARE
        src_username VARCHAR2(20) := '&1';
        src_password VARCHAR2(20) := '&2';
        src_connstring VARCHAR2(40) := '&3';
        dest_username VARCHAR2(20) := '&4';
        dest_password VARCHAR2(20) := '&5';
        dest_connstring VARCHAR(40) := '&6';
    
        CURSOR user_table_cur IS
            SELECT table_name
            FROM user_tables
            ORDER BY table_name DESC;
    
    BEGIN
        FOR user_table IN user_table_cur LOOP
            dbms_output.put_line('COPY FROM '
                || src_username ||'/'|| src_password ||'@'|| src_connstring
                || ' TO '
                || dest_username ||'/'|| dest_password ||'@'|| dest_connstring
                || ' APPEND ' || user_table.table_name
                || ' USING SELECT * FROM '
                || user_table.table_name ||';');
        END LOOP;
    END;
    /
    
    SPOOL OFF
    SET TERMOUT ON
    SET FEEDBACK ON
    
    @tmp_copy_commands
    
    EXIT 0;
    

    Moving even further away from your original question...

    You don't even need to use PL/SQL for this, unless you want to use dynamic SQL and EXECUTE IMMEDIATE. This will do the same as the earlier example:

    SET TRIMOUT ON
    SET TRIMSPOOL ON
    SET VERIFY OFF
    SET LINES 1024
    SET PAGES 0
    SET HEAD OFF
    
    SPOOL tmp_copy_commands.sql
    SET TERMOUT OFF
    SET FEEDBACK OFF
    
    SELECT 'COPY FROM &1./&2.@&3. TO &4./&5.@&6. APPEND '
        || table_name || ' USING SELECT * FROM ' || table_name || ';'
    FROM user_tables
    ORDER BY table_name DESC;
    
    SPOOL OFF
    SET TERMOUT ON
    SET FEEDBACK ON
    
    @tmp_copy_commands
    
    exit 0;
    
    0 讨论(0)
提交回复
热议问题