in reply to Executing multiple mysql commands in a single execute() call.

Are you sure that multiple SELECT statements make sense? Do all queries return the same data types in their columns? Maybe you just want a UNION statement?

SELECT * FROM Games WHERE Team_ID=@t_id AND Date>'2012-05-01' UNION SELECT * FROM Games WHERE Team_ID=@t_id AND Date between '2012-04-01' +and '2012-05-01' -- ...

Also, why do you want to use a variable there, instead of using a join clause? MySQL seems to have "eval" Google tells me, and the documentation also seems to support the idea of having a query return multiple resultsets. Maybe that solves your problem in a different way.

Replies are listed 'Best First'.
Re^2: Executing multiple mysql commands in a single execute() call.
by membender (Novice) on May 30, 2012 at 09:29 UTC

    I'm not sure I understand your questions, and you may have misunderstood mine. I want to set t_id from one table and then use it in many following queries to other tables. I could join or use nested selects in all the following queries, but there's no reason to do all that work every time, when the value of t_id stays the same!

    I checked out eval and it seems to be more complicated than what I need. I just want to set a variable and use it, after all.

      Why cannot you take the results from your SELECT Team_ID query, and put it into a Perl variable? You can then, without much trouble, use it in all your further queries by passing it to $sth->execute($team_id, $other, $params)

      What Corion was wondering about is, if you want to put multiple queries into one string and execute it, how would you read the results? How would you distinguish between the results of one query and the next one? (I don't think most database interfaces support multiple result sets.) How would jamming all the queries into a single string help readability?

      We'd like to see some code from you, especially on how you call the database. You are using prepared queries, aren't you?

        why not you push value in variable( scalar,array- if multiple) and then use further. This is easy step to go ahead. For example.
        my $result = qx!kfsql -S Servername -D database -F e -s , -c \"select +field1 from test where criteria = "C" and some_date \!= $processdate +order by field_time desc\"!; if want to push in array then.. chomp $result; my @array= ($result=~ s/\"//g); this will push all result value into variable and you can use through +out in program.
        Hope this will help.