Jump to content

How do I run multiple queries in one query call


Go to solution Solved by Barand,

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.

  • Solution

 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
Edited by Barand

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)
  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.