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

How do you write a compound Sql statement in perl. I want to excute three insert statements.
my $sql = "Insert into tableone values('1', '2', '3') my $nextsql = "Insert into tabletwo values('4', '5', '6') my $lastsql = "Insert into tablethree values('7', '8', '9') my $loadHandle = $dbh->prepare("$sql; $nextsql; $lastsql"); $loadHandle->execute || die "Could not execute SQL statement ... maybe invalid?";
Thanks in advance

2005-02-25 Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: Compound Sql Statement
by dragonchild (Archbishop) on Feb 25, 2005 at 13:59 UTC
    1. Have you tried your code? If you have, what error(s) did you run into?
    2. Your code does not compile as written. Your double-quotes aren't doing what you think they're doing. Did you cut'n'paste or retype?
    3. Not every database can handle multiple SQL statements. Not every DBD can handle multiple SQL statements, even if the database can. What database are you using?

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      I have tried the statements and get an error stating, "Can't call method "execute" on an undefined value". I assembled the sql by looking at examples. I am using an access database.
        Check if your prepare method call fails, and if so, see what the error is:
        my $loadHandle = $dbh->prepare("$sql; $nextsql; $lastsql") or die "Couldn't prepare statement: ",$dbh->errstr,"\n";
Re: Compound Sql Statement
by dbwiz (Curate) on Feb 25, 2005 at 15:30 UTC

    I have the feeling that you are using the wrong approach.

    What about using an array of values and just one SQL statement with placeholders?

    my @values = ( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 'a', 'b', 'c' ] ); my $sql = "Insert into tableone values(?, ?, ?); my $loadHandle = $dbh->prepare($sql); for (@values) { $loadHandle->execute(@$_) or die "Could not execute SQL statement $sql " . " with values ( @$_ )... $DBI::errstr\n "; }

    However, if what you want to do is execute different statements, here's another way:

    my @SQL_statements = ( "create table x(i int)", "insert into x values (1)", "update x set i = i * 10 " ); $dbh->begin_work(); for (@SQL_statements) { eval { $dbh->do($_)}; if ($@) { $dbh->rollback(); die "error executing query '$_', $DBI::errstr\n"; } } $dbh->commit();
      Thanks everyone and a special thanks to dbwiz for coming up with the solution that worked for me. doyle
Re: Compound Sql Statement
by jfroebe (Parson) on Feb 25, 2005 at 14:49 UTC

    The sql is generic enough... Perhaps this would work better for you:

    my $sql = "Insert into tableone values('1', '2', '3')"; my $nextsql = "Insert into tabletwo values('4', '5', '6')"; my $lastsql = "Insert into tablethree values('7', '8', '9')"; my $loadHandle = $dbh->prepare("$sql; $nextsql; $lastsql;"); $loadHandle->execute or die "Could not execute SQL statement ... maybe invalid?"; ....

    even better:

    my $sql = <<EO_SQL; insert into tableone values('1', '2', '3') insert into tabletwo values('4', '5', '6') insert into tablethree values('7', '8', '9') EOF_SQL my $loadHandle = $dbh->prepare($sql); $loadHandle->execute or die "Could not execute SQL statement $sql ... maybe invalid?"; ....

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

      Thanks Jason, when I run this code...
      $sql = <<EO_SQL; insert into tableone values('1', '2', '3') insert into tabletwo values('4', '5', '6') insert into tablethree values('7', '8', '9') EOF_SQL my $loadHandle = $dbh->prepare($sql); $loadHandle->execute or die "Could not execute SQL statement $sql ... maybe invalid?";
      I get an error stating, "Can't find string terminator "EO_SQL" anywhere before EOF"