sql mysql_parse_csv

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql mysql_parse_csv相关的知识,希望对你有一定的参考价值。

DELIMITER //

DROP PROCEDURE IF EXISTS explode_table //
CREATE PROCEDURE explode_table(bound VARCHAR(255), startstr INT, endstr INT)

BEGIN
DECLARE id INT DEFAULT 0;
DECLARE value TEXT;
DECLARE occurance INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE splitted_value VARCHAR(255);
DECLARE done INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT SUBSTRING(DATA, startstr, endstr) FROM table_name WHERE FILE_NAME LIKE 'some_like_%' LIMIT 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

DROP TEMPORARY TABLE IF EXISTS table2;
CREATE TEMPORARY TABLE table2(
    `value` VARCHAR(255) NOT NULL
) ENGINE=Memory;

OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO value;

IF done THEN
LEAVE read_loop;
END IF;

SET occurance = (SELECT LENGTH(value) - LENGTH(REPLACE(value, bound, '')) +1);
SET i=1;
SET id=1;
    WHILE i <= occurance DO
    SET splitted_value = TRIM(
        (SELECT REPLACE(SUBSTRING(SUBSTRING_INDEX(value, bound, i),
            LENGTH(SUBSTRING_INDEX(value, bound, i - 1)) + 1), ';', ''))
    );
    INSERT INTO table2 VALUES (splitted_value);
    SET i = i + 1;
    END WHILE;
END LOOP;

-- return result
SELECT * FROM table2;

CLOSE cur1;
END; //
DELIMITER ;

-- How to use:
CALL explode_table(';', 1, 800);

以上是关于sql mysql_parse_csv的主要内容,如果未能解决你的问题,请参考以下文章

sql 2008和sql 2012的区别

sql [sql技巧]一些sql技巧#sql

sql 2008 r2 在sql 2008上兼容么

SQL基础

有大神知道,sql server 中如何批量执行sql语句吗?

pl sql developer怎么执行sql