Solved

MySQL UPDATE using a *dynamic* list of values

Posted on 2013-12-22
2
853 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_
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now