http://www.perlmonks.org?node_id=992265


in reply to Multiple SQL statements in DBI

Your question 1 is answered by the likely fact that the statement sequence (as a whole) was either not executed until it parsed correctly, or was run in a transaction, and thus all modifications were rolled back.

2. Executing multiple SQL statements in one ->do call is dependent on the database driver. I wouldn't rely on it.

3. It depends on what the database driver implements. Ask the database vendor about the rationale.

Replies are listed 'Best First'.
Re^2: Multiple SQL statements in DBI
by tel2 (Pilgrim) on Sep 07, 2012 at 22:30 UTC
    Thanks for your answers, Corion.  I'm using MySQL, but I don't think MySQL is preventing multiple statements, since from the Linux prompt I can echo multiple SQL commands and pipe them to a mysql command and they work.  From looking below at chacham's answer, it looks as if DBI prevents it, which is fine by me.

      It's the mysql client library C API that introduces the limitation:

      Executes the SQL statement pointed to by the null-terminated string stmt_str. Normally, the string must consist of a single SQL statement without a terminating semicolon (“;”) or \g.

      Special steps must be taken to allow execution of multiple statements, and seems to be new in version 5.0.

      The Postgresql C API doesn't follow that model, but makes another interesting remark:

      Note however that the returned PGresult structure describes only the result of the last command executed from the string

      which makes it less useful to run multiple statements in a single API call.

        Months later...

        Thanks for that, moritz.  Just noticed your post recently.

        tel2

      I don't think DBI prevents anything. It passes the text you give it to the database to parse. If the mysql command line takes multiple statements, then the command line tool is likely parsing separate statements. Likewise, in Oracle, you can pass multiple SQL statements to sqlplus, but not to DBI, because sqlplus does some parsing of its own.

      I do see that the latest version of DBD::mysql supports multiple result sets. You might want to look into that.

        Thanks for that runrig, but why do you say "I don't think DBI prevents anything", when as chacham has pointed out below, the 'DBI documentation states "Multiple SQL statements may not be combined in a single statement handle ($sth), although some databases and drivers do support this (notably Sybase and SQL Server)."'.  To me, that means, regardless of whether the database driver supports it, DBI will prevent it.  Or am I misunderstanding something?