red-beard has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to run several execute statements in a prepare statement like you would in a Mysql template.

So for example I would like to execute a create table, insert data into it, and select that information all within a single prepare statement or something similar?

The reason for wanting to do this is a vendor has setup a mysql database that heavily relies on templates for reporting. I would like to use those templates with minimal changes.

Replies are listed 'Best First'.
Re: Mysql templates
by graff (Chancellor) on Nov 08, 2005 at 05:47 UTC
    I believe that DBI and DBD::mysql will not allow you to combine distinct statements (e.g. with an SQL string containing ";" to delimit two or more separate operations) in a single prepare.

    But I expect it would be fairly simple to set up the perl script so that it reads a template, splits it into its component single statements, and then prepares and executes each in turn. That way, the templates themselves don't need to be changed -- you just need to be scrupulous about how you employ them within a perl script.

    But then, I've never used mysql templates, and have never even seen one. If you can provide a simplified example of what such a template would look like, this would be instructive.

      Basically a template is a file that contains one or more distinct statements ending with a ";"... at least that's what is being used by this reporting program.

      In this file it would contain...

      optimize table test; select * from test;
        So the only "tricky" part is to be able to say, for each operation within a template file, whether it needs to be handled via "do", or via "fetch" -- which might not be all that tricky, really, because the latter type will generally start with "select" (or "describe", or "show") as the first word of the statement.

        In general, you just need to split the template on semicolons (though there is the obvious -- though perhaps unlikely -- risk that a semicolon might appear as data within one of the operations, meaning you have to be careful about how you do the split.

        If the files are consistently formatted as you showed in this brief example (each distinct operation / statement is on a separate line, so that statements are separated by ";\n"), then splitting is much simpler -- you could even set your input record separator like this:  $/ = ";\n"; so that you read one statement at a time from the template, check what sort of statement it is, and pass it to the appropriate DBI method.