Array variable in mysql

Is there a way in a MySQL script to declare an array (or any collection) and loop into it to do something?

For example,

SET @myArrayOfValue=[2,5,2,23,6] for each @value in @myArrayOfValue INSERT INTO EXEMPLE VALUES(@value, 'hello'); end for each 
+6
sql mysql
Sep 16 '09 at 16:14
source share
2 answers

No, SQL does not support FOR EACH / etc syntax. Closest you could use cursors. In addition, there is no array syntax in SQL - you will have to use:

 SELECT 2 FROM DUAL UNION ALL SELECT 34 FROM DUAL UNION ALL SELECT 24 FROM DUAL 

... build your "array of values" equivalent in SQL.

SQL scripts will have separate INSERT statements. Would you like to use PHP / Java / etc. use the FOR loop-esque syntax like what is shown in your example.

+2
Sep 16 '09 at 16:26
source share

Could you use something like MySQL SET ? instead of scrolling, you can save the values ​​in SET.

0
Sep 16 '09 at 16:32
source share



All Articles