<p>I would like to create a stored procedure in MySQL that took a list as argument. For example, say that I would like to be able to set multiple tags for an item in one call, then what I want to do is to define a procedure that takes the ID of the item and a list of tags to set. However, I can't seem to find any way to do this, there is no list datatype, as far as I'm aware, but can it be emulated somehow? Could the list of tags be a comma-separated string, which can somehow be split and looped over?</p>

<p>How do you usually work with lists in MySQL stored procedures?</p>

[{'answer_id': 8820, 'author': "Shane O'Grady", 'author_id': 1115, 'author_profile': 'https://Stackoverflow.com/users/1115', 'pm_score': 0, 'selected': False, 'text': '<p>Not sure if these will work specifically in a SP, but there are ENUM and SET datatypes in MySQL 5 which may do what you need.\n<a href="http://dev.mysql.com/doc/refman/5.0/en/enum.html" rel="nofollow noreferrer">http://dev.mysql.com/doc/refman/5.0/en/enum.html</a>\n<a href="http://dev.mysql.com/doc/refman/5.0/en/set.html" rel="nofollow noreferrer">http://dev.mysql.com/doc/refman/5.0/en/set.html</a></p>\n'}, {'answer_id': 8823, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 0, 'selected': False, 'text': '<p>In my programming language of Choice, C#, I actually do this in the application itself because split() functions and loops are easier to program in C# then SQL, However!</p>\n\n<p>Perhaps you should look at <a href="http://www.java2s.com/Tutorial/MySQL/0460__String-Functions/SUBSTRINGINDEXstrdelimcountReturnsthesubstringfromstringstrbeforecountoccurrencesofthedelimiterdelim.htm" rel="nofollow noreferrer">SubString_Index()</a> function.</p>\n\n<p>For example, the following would return google:</p>\n\n<pre><code>SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(\'www.google.com\', \'.\', -2), \'.\', 1);\n</code></pre>\n'}, {'answer_id': 8837, 'author': 'Clyde', 'author_id': 945, 'author_profile': 'https://Stackoverflow.com/users/945', 'pm_score': 1, 'selected': False, 'text': '<p>Depending on how complicated you want to get, you can use a generic linking table.  For one of my applications there are several reports where the user might pick, for instance a list of customers to run the report on rather than just a single customer from a combo box.  I have a separate table with 2 fields:</p>\n\n<ul>\n<li>UniqueID (guid)</li>\n<li>ItemID</li>\n</ul>\n\n<p>The psuedo-code looks like this:</p>\n\n<pre><code>GUID guid = GenerateGUID()\ntry\n  for each customer in customerList { INSERT(guid, customerId) }\n  ExecuteSQLPocedure(guid)\n  --the procedure can inner-join to the list table to get the list\nfinally\n  DELETE WHERE UniqueID=guid      \n</code></pre>\n'}, {'answer_id': 8859, 'author': 'roo', 'author_id': 716, 'author_profile': 'https://Stackoverflow.com/users/716', 'pm_score': 4, 'selected': True, 'text': '<p><a href="http://forums.mysql.com/read.php?98,50521,50521#msg-50521" rel="noreferrer">This</a> article has some good discussion on the problem of parsing an array to a stored procedure since stored procedures only allow valid table column data-types as parameters.</p>\n\n<p>There are some neat things you can do with the <a href="http://dev.mysql.com/tech-resources/articles/csv-storage-engine.html" rel="noreferrer">csv</a> table type in mysql - that is if you are loading a flat file into the db.</p>\n\n<p>You could create a temporary table in the stored procedure, iterate over the csv list and insert it to the temp table, then create a cursor which selects the values from that table. This <a href="http://forums.mysql.com/read.php?98,50521,184245#msg-184245" rel="noreferrer">answer</a> in the above mentioned thread shows a way of doing this.</p>\n\n<p>Generally I would split the array before I come to the database and then perform the query individually on each item.</p>\n'}]
['https://Stackoverflow.com/questions/8795', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1109/']