title: CONCAT string and INT in MYSQL and export big mysql table to small csv files
body: <p>I am new in MYSQL programming. I want to select into outfile a very big table into several csv files using MYSQL loop script. My script is as following:</p>
  
  <pre><code>BEGIN
          SET @t_lines=0;
          SET @t_count=0;
          SET @t_filepath='/home/ab/path/table_name_02212013_';
          WHILE t_lines&lt;=5000000
          SET @t_filename=CONCAT(@t_filepath,CAST(@t_count as CHAR));
          select * into outfile @t_filename fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' from table_name limit @t_lines,@t_lines+300000;
          SET @t_lines= @t_lines+300000;
          SET @t_count= @t_count+1;
          END WHILE;
          COMMIT;
  END
  </code></pre>
  
  <p>I got a syntax error in line:</p>
  
  <blockquote>
    <p>SET @t_filename=CONCAT(@t_filepath,CAST(@t_count as CHAR));</p>
  </blockquote>
  
  <p>The syntax error caused by WHILE ... DO. Thanks for quick reply.
  Here I still have syntax error at "@t_lines,@t_lines+300000" after "limit". 
  I figure it out, seems "limit" not allowed "@t_lines+300000" to give the line range. It can be fixed by giving a new variable:</p>
  
  <pre><code>   BEGIN
              DECLARE t_lines INT DEFAULT 0;
              DECLARE t_count INT DEFAULT 0;
              DECLARE t_endlines INT DEFAULT 300000;
              DECLARE t_linerange INT DEFAULT 300000;
              SET @t_filepath='/home/ab/path/table_name_02212013_';
              WHILE t_lines&lt;=5000000 DO
              SET @t_filename=CONCAT(@t_filepath,CAST(@t_count as CHAR));
              select * into outfile '@t_filename' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' from table_name limit t_lines,t_endlines;
              SET t_lines= t_lines+t_linerange;
              SET t_endlines= t_endlines+t_linerange
              SET t_count= t_count+1;
              END WHILE;
              COMMIT;
      END
  </code></pre>
  
  <p>Thanks very much</p>