Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6547715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:51:53+00:00 2026-05-25T11:51:53+00:00

I have a small PL/SQL script that I’m using to try and copy data

  • 0

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):

sqlplus username/password@server.com:1434/SERVICENAME @copyTables.sql source_username source_password source_connstring destination_username destination_password destination_connstring

The copyTables.sql script:

SET SERVEROUTPUT ON;
DECLARE
  source_username VARCHAR2(20) := &1
  source_password VARCHAR2(20) := &2
  source_connstring VARCHAR2(2) := &3
  destination_username VARCHAR2(20) := &4
  destination_password VARCHAR2(20) := &5
  destination_connstring VARCHAR(20) := &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(source_username);
    dbms_output.put_line(user_table.table_name);
    COPY FROM {source_username}/{source_password}@{source_connstring} TO {destination_username}/{destination_password}@{destination_connstring} APPEND user_table.table_name user_table.table_name USING SELECT* FROM user_table.table_name;
  END LOOP;
END;

The only issue is that when I run this, it seems to misinterpret a colon (:) in the connection string for something to do with bind variables:

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

old   2:   source_username VARCHAR2(20) := &1
new   2:   source_username VARCHAR2(20) := SANITISED
old   3:   source_password VARCHAR2(20) := &2
new   3:   source_password VARCHAR2(20) := SANITISED
old   4:   source_connstring VARCHAR2(2) := &3
new   4:   source_connstring VARCHAR2(2) := server.com:3630/SANITISED
old   5:   destination_username VARCHAR2(20) := &4
new   5:   destination_username VARCHAR2(20) := SANITISED
old   6:   destination_password VARCHAR2(20) := &5
new   6:   destination_password VARCHAR2(20) := SANITISED
old   7:   destination_connstring VARCHAR(20) := &6
new   7:   destination_connstring VARCHAR(20) := server.com:3630/SANITISED
SP2-0552: Bind variable "3630" not declared.
SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

I’ve already escape the above with braces ({}), but it still seems to complain about bind variables.

Also – as a addendum – the way I’m doing above, is this the best practice in passing command-line arguments through to a PL/SQL script? I’m open to suggestions on better methods of doing this.

Cheers,
Victor

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:51:54+00:00Added an answer on May 25, 2026 at 11:51 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small SQL script that I'm executing with Oracle's SQL*Plus to emulate
I have a small test database I'm using for learning SQL. There's a Duel
I have a small SQL CE 4.0 database with several tables, mapped using Entity
I have a somewhat small database in SQL Server Express 2005 that I really
I have a small VB.NET application that I'm working on using the full version
I've created a small Bash script that does a MySQL data dump. Because the
I am new to sql. i have a small question. i have two tables
I'm useing SQL Server 2008 and I have a small script to split a
SQL to find duplicate entries (within a group) I have a small problem and
I have many small files containing code fragments, pseudo-code algorithms, classes, templates, SQL-samples, etc.,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.