Execute SQL file in Perl

前端 未结 4 441
一向
一向 2020-12-10 17:53

We have a Perl script which runs a SQL and puts data in the table. Now instead of supplying a single SQL statement, we want to pass bunch of them putting them together in a

4条回答
  •  醉酒成梦
    2020-12-10 18:46

    There is a sort of workaround for DDL. You need to slurp SQL file first and then enclose it's contents into BEGIN ... END; keywords. Like:

    sub exec_sql_file {
        my ($dbh, $file) = @_;
    
        my $sql = do {
            open my $fh, '<', $file or die "Can't open $file: $!";
            local $/;
            <$fh>
        };
    
        $dbh->do("BEGIN $sql END;");
    }
    

    This subroutine allows to run DDL (SQL) scripts with multiple statements inside (e.g. database dumps).

提交回复
热议问题