Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MySQL UPDATE using a *dynamic* list of values

Posted on 2013-12-22
2
Medium Priority
?
891 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this article, we’ll look at how to deploy ProxySQL.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

715 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