Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Multiple SQL statements in DBI

by Corion (Pope)
on Sep 07, 2012 at 08:29 UTC ( #992265=note: print w/replies, xml ) Need Help??

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 (Monk) 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.


      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?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992265]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-12-16 12:29 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (450 votes). Check out past polls.