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

I'm playing around with a PERL TK GUI that displays a directory of SQL scripts, allows viewing and/or modification of a specific script and also accesses the database (Oracle 8i) to run the SQL script. Everything is fine except running the script. If I log in directly to the database and use "start SQLscriptname" where SQLscriptname is a series of SQL commands in a file, I get the desired results. Now, if I attempt to use DBI prepare and execute from within my GUI, I get an Invalid SQL command error for each line in the file. The code looks like:
sub &run_SQL_script { $dbh = DBI->connect($database, $username, $password) or die "Can't connect to database: $DBI::errstr\n"; open (SCRIPT, "$filename"); while (my $data = <SCRIPT>) { $SQLscript = $dbh->prepare(" '$data' "); $SQLscript->execute(); } close (SCRIPT); }

Replies are listed 'Best First'.
Re: SQL Scripts with PERL
by duff (Parson) on Dec 31, 2003 at 18:24 UTC

    It appears that you have a copious extra quotes in your prepare statement. Removing them should help.

    Also, you're not checking the return value of your open.

Re: SQL Scripts with PERL
by mpeppler (Vicar) on Dec 31, 2003 at 18:38 UTC
    DBI abd DBD::Oracle can only execute one SQL command per prepare() call. If I'm guessing correctly your SQL script files contain multiple SQL statements, in which case you will have to parse your SQL script file so that you execute each command separately.

    Michael

      No. The OP script is preparing and executing each line of the external file.

      Unless a line contains more than one statement, it seems to me that the only mistake is the one outlined by duff.