ProfessorHaroldHill has asked for the wisdom of the Perl Monks concerning the following question:

I want to SELECT * FROM foo
from a table & insert those rows into an identical table bar

The table name can alter
Is there a slick way to do this without having to extract all the column names for the insert clause. regards Kevin

Replies are listed 'Best First'.
Re: DBI question
by busunsl (Vicar) on May 09, 2001 at 17:30 UTC
    Try:
    insert into bar select * from foo
    Should work with any major RDBMS.
      Yep, but the tables are on different databases hence DBI, with two diffent dbhandles. Sorry, I should have mentioned that .. :)
        You can build the insert string with the NAME attribute of the statement handle.
        From the docs:
        `NAME' (array-ref, read-only) Returns a reference to an array of field names for each column. The names may contain spaces but should not be truncated or have any trailing space. Note that the names have the letter case (upper, lower or mixed) as returned by the driver being used. Portable applications should use the NAME_lc entry elsewhere in this document or the NAME_uc entry elsewhere in this document. print "First column name: $sth->{NAME}->[0]\n";
Re: DBI question
by DeaconBlues (Monk) on May 09, 2001 at 17:57 UTC

    This is more of a SQL question than a DBI question. For inserting rows you can skip the column names if you specifiy values in order and for every column.

    Here is the format:

    insert into table values (val1, val2, val3, ... valN);

    where table has N columns

    you must have a value for each column, even if that value is NULL(undef).

      If you can't use native utilities for import/export (bcp?)
      I would suggest to use bind columns for speed reasons...
      (see this excellent node...)

      "Only Bad Coders Badly Code In Perl" (OBC2IP)
      A bit of context here, I am writing an Ingres module that takes a list of many tables and builds copies of the tables on another database the number of rows being dependant on a passed "where clause".
      So I thought that as DBI knows the column names on a "select *" it could automatically use them on an insert to an identical table( same no of columns, same names), without jiggery pokery.
      as in INSERT INTO foo ( SELECT * FROM bar)
      Not so.... ???
        If the columns and names are the same then, as earlier suggest,  insert into foo select * from bar will work fine.

        Depending on your RDBMS and how things are setup you may be able to specify a table in a different database as database:bar or some such, but this can run into problems with transactions etc.

        If you're doing bulk copies, it's probably faster to:

        • dump table contents into text files
        • turn off logging on target database
        • load text files into target database tables (sqlloader for oracle, load for informix etc)

        Depending on volume of course...

Re: DBI question
by zigster (Hermit) on May 09, 2001 at 17:49 UTC
    For sybase there is a utility bcp for bulk copying of data between databases. I guess otherdatabases have similar loading schemes. What database are you using?
    --

    Zigster