Solved

MySQL UPDATE using a *dynamic* list of values

Posted on 2013-12-22
2
874 Views
Last Modified: 2013-12-22
I need to create a stored proc that accepts a list of one or more id's (example "1,12,13,98") then uses those ids' in a simple UPDATE. Obviously this sql wouldn't work, but it demonstrates the idea:

    
       CREATE PROCEDURE MyProc ( listOfIDs VARCHAR(...) )  
       AS   
       BEGIN
                UPDATE SomeTable
                SET    SomeFlag = 1
                WHERE  IDColumn IN ( listOfIDs )
       END
      

Open in new window


I already know I can use dynamic SQL for this, but ... I'd like to avoid that if possible. Are there any other options for MySQL?

For example, in SQL Server I could use xml functions. Just pass in the id's as an xml string the use a regular JOIN like below. Does mySQL have anything similar?

      DECLARE @str XML
      SET @str = '<id>1</id><id>12</id>....'
       

      ; WITH t AS (
            SELECT ParamValues.id.value('.', 'INTEGER') AS ID
            FROM   @str.nodes('//id') ParamValues(id)
      )
      SELECT *
      FROM   other Table ot INNER JOIN t ON t.id = ot.id

Open in new window

0
Comment
Question by:_agx_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 39735277
Try this, passing it a comma delimited list of id's in a string:
    
       CREATE PROCEDURE MyProc ( listOfIDs TEXT)  
       AS   
       BEGIN
                UPDATE SomeTable
                SET    SomeFlag = 1
                WHERE  FIND_IN_SET(IDColumn, listOfIDs) > 0
       END
      
                                  

Open in new window

Documentation here:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set

Disclaimer: I haven't tested this, so ensure you have a backup of your data.
0
 
LVL 52

Author Comment

by:_agx_
ID: 39735284
Thanks, I'd completely forgotten about that function!  I can confirm it works.

I also found another (more complex) method using a lookup table. Now to compare the query plans :)
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question