Jump to content

How do I run multiple queries in one query call


fastsol

Recommended Posts

I am using PDO and have the PDO::ATTR_EMULATE_PREPARES => false set.  So it doesn't seem to allow for multiple query strings in a single query call.  Is there a way around this or a different method that is used in such circumstances?  I don't believe transactions is what I'm looking for cause the queries  wouldn't be dependent on each other in a certain order.  Just more for multiple INSERT or UPDATE kind of things.

 Just more for multiple INSERT or UPDATE kind of things.

 

Multiple insert

INSERT INTO tablename (col1, col2) VALUES
(1, 'AAA'),(2,'BBB'),(3,'CCC');

Multiple update

UPDATE tablename
SET col2 =
    CASE id
         WHEN 1 THEN 'DDD'
         WHEN 2 THEN 'EEE'
         WHEN 3 THEN 'FFF'
         ELSE col2
   END

Barry, I think he is looking for stored procedures or trigers. fastsol, stored procedures provide SQL enhancements supporting variables, comments, exceptions, conditional testing and looping as programming elements.

and the multi-value update can be implemented as the following without having to dynamically produce case logic in the query (assuming that id is a unique index) -

INSERT INTO tablename (id,col2) VALUES (1,'AAA'),(2,'BBB'),(3,'CCC')
ON DUPLICATE KEY UPDATE col2=VALUES(col2)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.