日期:2014-05-16  浏览次数:20764 次

mysql存储过程分割字符串
DROP PROCEDURE IF EXISTS split_string;
CREATE PROCEDURE split_string(IN to_split VARCHAR(255), IN split_with VARCHAR(100))
BEGIN
  DECLARE total_length INT;
  DECLARE location INT;
  DROP TEMPORARY TABLE IF EXISTS temp_store;
  CREATE TEMPORARY TABLE temp_store(str VARCHAR(100));

  WHILE LENGTH(to_split) > 0  DO
    SET total_length = LENGTH(to_split);
    SET location     = LOCATE(split_with, to_split);
    IF location = 0 THEN
      INSERT INTO temp_store(str) VALUE(to_split);
      SET to_split = '';
    ELSE   
      INSERT INTO temp_store(str) VALUE(LEFT(to_split, location-1));
      SET to_split = RIGHT(to_split, total_length-location);
    END IF;  
  END WHILE;

  SELECT * FROM temp_store;
end