Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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.


Comment on Re: Multiple SQL statements in DBI
Download Code
Re^2: Multiple SQL statements in DBI
by tel2 (Scribe) 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.

      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?

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992265]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-09-23 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls