insert multiple rows using while loop

I would like to insert multiple rows into oracle table.

script runs and completes no error messages but it doesnt do the insert.

use DBI;
use strict;
      #create connection
my $wdat2ghw = DBI->connect('DBI:Oracle:wdat2ghw', 'student', 'learn')
    || die "Database connection not made: $DBI::errstr";
      #create and execute query
my $strsql = "select employee_id, name  from employee";
my $result = $wdat2ghw->prepare($strsql);
      $result->execute or die "Couldn't execute: " . $wdat2ghw->errstr . "\n";
my @result = $result-> fetchrow_array;

while(@result = $result->fetchrow_array){
    #insert 1 record
     "insert into customer (customer_id, name) values ('@result')";
  };
 
  $result->finish;
  $wdat2ghw->disconnect;
 print "All done";

D
deNZityAsked:
Who is Participating?
 
Kim RyanIT ConsultantCommented:
use DBI;
use strict;
     #create connection
my $wdat2ghw = DBI->connect('DBI:Oracle:wdat2ghw', 'student', 'learn')
    || die "Database connection not made: $DBI::errstr";
     #create and execute query
my $strsql = "select employee_id, name  from employee";
my $result = $wdat2ghw->prepare($strsql);
     $result->execute or die "Couldn't execute: " . $wdat2ghw->errstr . "\n";
my @result = $result-> fetchrow_array;

# You still need to prepare and execute a statement for the insertion
# But no need to loop on @result, just extract the 2 elements one at a time
$strsql = "insert into customer (customer_id, name) values ('$result[0]','$result[0')";
$result = $wdat2ghw->prepare($strsql);
$result->execute or die "Couldn't execute: " . $wdat2ghw->errstr . "\n";

  $result->finish;
  $wdat2ghw->disconnect;
 print "All done";

0
 
deNZityAuthor Commented:
Thanks for the reply, I wondered wether or not I needed to prepare and execute
the sql.
I used ('$result[0]','$result[1]')";

points to you.
0
 
Kim RyanIT ConsultantCommented:
Glad to help, yes  my error, result[1] is correct. You can also fetch into a hash and the used named parameters like $result{employee_id}.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.