viernes, 10 de junio de 2016

Primer procedimiento almacenado respetable

delimiter $$
DROP PROCEDURE IF EXISTS sp_splitx$$
CREATE PROCEDURE sp_splitx(str nvarchar(6500), dilimiter varchar(15), tmp_name varchar(50))
BEGIN
declare end_index   int;
declare part        nvarchar(6500);
declare remain_len  int;

set end_index      = INSTR(str, dilimiter);

while(end_index   != 0) do


    /* Split a part */
    set part       = SUBSTRING(str, 1, end_index - 1);
    select concat('',part);

    /* insert record to temp table */
    #call `sp_split_insert`(tmp_name, part);

    set remain_len = length(str) - end_index;
    set str = substring(str, end_index + 1, remain_len);

    set end_index  = INSTR(str, dilimiter);

end while;

if(length(str) > 0) then
    select concat('',str);

    /* insert record to temp table */
    #call `sp_split_insert`(tmp_name, str);

end if;
END
$$

# probando con call  sp_splitx('c,d,f,g',',','x');

No hay comentarios:

Publicar un comentario