MySql, split a string and insert into table

后端 未结 4 566
再見小時候
再見小時候 2021-01-04 20:11

I have two inputs for my stored procedure. One is the \'RoledID\' and second one is the \'MenuIDs\'. \'MenusIDs\' is a list of comma separated menus ids that need to be inse

相关标签:
4条回答
  • 2021-01-04 20:21

    Give this a go. It may need some tweaking if the MenuIDs string does not conform to 'menuId,menuId,menuId'.

    Also I do not know what data type the menuId column is in your target table (INT?) so you may have to put some numeric checking in too (in case '1,2,3,banana,4,5' is passed in as the MenuIds input parameter).

    DELIMITER $$
    
    DROP PROCEDURE IF EXISTS `insert_role_menuids`$$
    
    CREATE PROCEDURE `insert_role_menuids`(IN RoleID INT,IN MenuIDs varchar(500))
    BEGIN
    declare idx,prev_idx int;
    declare v_id varchar(10);
    
    set idx := locate(',',MenuIDs,1);
    set prev_idx := 1;
    
    WHILE idx > 0 DO
     set v_id := substr(MenuIDs,prev_idx,idx-prev_idx);
     insert into RolesMenus (RoleId,MenuId) values (RoleID,v_id);
     set prev_idx := idx+1;
     set idx := locate(',',MenuIDs,prev_idx);
    END WHILE;
    
    set v_id := substr(MenuIDs,prev_idx);
    insert into RolesMenus (RoleId,MenuId) values (RoleID,v_id);
    
    END$$
    DELIMITER ;
    
    0 讨论(0)
  • 2021-01-04 20:21

    AFAIK MySQL does not have a function to split strings. Here is the MySQL manual for string related functions. In the comments section should be some information about workarounds for splitting string with substring-functions but not really usable: MySQL manual

    0 讨论(0)
  • 2021-01-04 20:40

    You can build one INSERT query (because statement allows to insert multiple records) and run it with prepared statements, e.g. -

    SET @MenuIDs = '1,2,3';
    SET @RoledID = 100;
    
    SET @values = REPLACE(@MenuIDs, ',', CONCAT(', ', @RoledID, '),('));
    SET @values = CONCAT('(', @values, ', ', @RoledID, ')'); -- This produces a string like this -> (1, 100),(2, 100),(3, 100)
    
    SET @insert = CONCAT('INSERT INTO RolesMenus VALUES', @values); -- Build INSERT statement like this -> INSERT INTO RolesMenus VALUES(1, 100),(2, 100),(3, 100)
    
    -- Execute INSERT statement
    PREPARE stmt FROM @insert;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    As you see, it can be done without stored procedure.

    0 讨论(0)
  • 2021-01-04 20:44

    for this solution, you must create a table with the name split_table, it can have a id(autoincrement) if you need it and must have a column where to store the value (I call it valor)

    DELIMITER $$
    
    USE `dbaname`$$
    
    DROP PROCEDURE IF EXISTS `Split`$$
    
    CREATE DEFINER=`root`@`localhost` PROCEDURE `Split`(
        IN cadena VARCHAR(8000),
        IN delimitador VARCHAR(10)
        )
    BEGIN
    
        TRUNCATE split_table;
    
        SET @posicion = 1;
        SET @ldel = LENGTH(delimitador);     
        SET @valor = SUBSTRING_INDEX(cadena, delimitador, 1);
    
        WHILE @valor <> '' AND @posicion > 0 DO
    
            SET @valor = SUBSTRING_INDEX(cadena, delimitador, 1);
    
            INSERT INTO split_table(valor) VALUES (@valor);
    
            SET @posicion = POSITION(delimitador IN cadena);
            SET @largo = LENGTH(cadena);
    
            IF @largo >= @posicion THEN
                SET cadena = SUBSTR(cadena, @posicion + @ldel, @largo - @posicion);
                SET @valor = SUBSTRING_INDEX(cadena, delimitador, 1);
            ELSE
                SET @posicion = 0;
            END IF;
    
        END WHILE;
    
        END$$
    
    DELIMITER ;
    
    0 讨论(0)
提交回复
热议问题