Mysql CSV to Insert CSV

Peter Downie.com Library My Mysql Functions

Basically this function allows you to take a csv value of 1,2,3 and convert it to ('1'),('2'),('3') for an insert statement.

DELIMITER //
DROP PROCEDURE IF EXISTS csvToInsertValues//
CREATE PROCEDURE csvToInsertValues(IN INcsv TEXT, OUT OUTcsv TEXT)
function: BEGIN
    DECLARE varCurrentPosition BIGINT UNSIGNED DEFAULT 0;
    DECLARE varInCsvLength BIGINT UNSIGNED;
    DECLARE varCurrentCharacter CHAR(1);
    DECLARE varPreviousText TEXT DEFAULT '';
    SET varInCsvLength = CHAR_LENGTH(INcsv);
    SET OUTcsv = '';
    csvLoop: LOOP
        SET varCurrentPosition = varCurrentPosition + 1;
        SELECT SUBSTRING(INcsv,varCurrentPosition,1) INTO varCurrentCharacter;
        IF varCurrentCharacter != ',' THEN
            SET varPreviousText = CONCAT(varPreviousText,varCurrentCharacter);
        ELSE
            SET OUTcsv = CONCAT(OUTcsv,'(\'',varPreviousText,'\')');
            SET varPreviousText = '';
        END IF;
        if varCurrentPosition >= varInCsvLength THEN
            IF varPreviousText != '' THEN
                SET OUTcsv = CONCAT(OUTcsv,'(\'',varPreviousText,'\')');
            END IF;
            LEAVE csvLoop;
        END IF;
    END LOOP csvLoop;
END //
DELIMITER ;