MySQL通过存储过程将字符串拆分为行

时间:2014-02-03 11:01:42

标签: mysql stored-procedures

我需要在MYSQL中编写存储过程以将给定的CSV字符串转换为行。

E.g. Input String: 'str1,str2,str3,str4’

Output String: String
          ________
        str1
        str2
        str3
        str4

我有mssql片段,我试图转换为简单的mysql程序:

CREATE PROCEDURE CSVtoROWS(IN S VARCHAR(100))
BEGIN
Declare  ISDefault varchar(20) DEFAULT '';
Declare str varchar(100);

set str= LTRIM(RTRIM(S));

IF(str = '')
   begin 
    SET ISDefault = 'string is empty';
    return(1);
end;
ELSE
    begin 
        SELECT Split.a.value('.', 'VARCHAR(100)') AS Str
        FROM  (SELECT CAST ('<M>' + REPLACE(str, ',', '</M><M>') + '</M>' AS XML) AS Str ) AS A 
        CROSS APPLY Str.nodes ('/M') AS Split(a);
    end;
END;

0 个答案:

没有答案