MySQL CSV Row to multiple Rows - mysql

I need to migrate an old database to my new one. Unfortunately the guy who wrote the old database created an n,n relation using a field with comma separated foreign keys.
I would like to write a mysql query (maybe using insert into ... select) that splits those comma seperated foreign keys so that i can build a table where each row is a foreign key.
Is this possible?

It's not straightforward to do this in pure SQL. It will be easiest to retrieve each record in turn using a programming language of your choice and insert the many-to-many join table records based on the comma separated field. The following pseudo code suggests an approach that you might use:
for each (id, csv_foreign_keys) in source_rows do
foreign_keys = split ',', csv_foreign_keys
for each fk in foreign_keys do
insert (id, fk) into many-to-many link table
Once you've done this, the existing column holding the comma separated foreign keys can be removed.

My solution
DELIMITER $$
DROP FUNCTION IF EXISTS SPLITCVS $$
DROP PROCEDURE IF EXISTS MIGRATE $$
CREATE FUNCTION SPLITCVS (
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '') $$
CREATE PROCEDURE MIGRATE ()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE id INT(11);
DECLARE csv BLOB;
DECLARE cur CURSOR FOR SELECT uid,foreigns FROM old;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
read_loop: LOOP
FETCH cur INTO id, csv;
IF done THEN
LEAVE read_loop;
END IF;
IF LENGTH(csv) <> 0 THEN
SET #i = 0;
SET #seps = LENGTH(csv) - LENGTH(REPLACE(csv, ',', ''));
IF RIGHT(csv,1) <> ',' THEN
SET #seps = #seps + 1;
END IF;
WHILE #i < #seps DO
SET #i = #i + 1;
INSERT INTO db.newtable(uid_local,uid_foreign)
VALUES (id,SPLITCVS(csv,',',#i));
END WHILE;
END IF;
END LOOP;
CLOSE cur;
END $$
CALL MIGRATE() $$
DROP FUNCTION SPLITCVS $$
DROP PROCEDURE MIGRATE $$

Related

Created a procedure for merging two tables branch_mstr and new_branches (having only two fields and same primary key)

delimiter $$
create procedure mergeData()
begin
declare counter, rowws int default 0;
set rowws = (select count(*) from new_branches);
begin
while counter < rowws do
if new_branches.branch_no != branch_mstr.branch_no then
insert into branch_mstr value(new_branches.branch_no, new_branches.name);
end while;
end;
end$$
delimiter ;
Why these crosses are showing, I am using MySQL and new to the PL/SQL. Asking for the help

How to set MySQL parameter multiple values in variable

I'm trying to assign multiple values in a variable and execute a query using it. For example below:
SET #ledger = "'Cash','Special Offer'";
SELECT `_ledger` FROM `acc_ledger` WHERE `_ledger` IN(#ledger);
But this doesn't work as planned. Is there a way to define multiple values in a variable? If yes, how? If no, can I have a suggestion on how to tackle this issue?
You can pass multiple values with comma separated and then split those variables int table and perform a join
create function to split comma separated parameter
DELIMITER $$
DROP FUNCTION IF EXISTS `SPLIT_STR` $$
CREATE FUNCTION SPLIT_STR(id_list VARCHAR(500), delimeter VARCHAR(10), position INT)
RETURNS VARCHAR(10)
DETERMINISTIC
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(id_list, delimeter, position),
LENGTH(SUBSTRING_INDEX(id_list, delimeter, position - 1)) + 1),
delimeter, '');
END$$
DELIMITER ;
call SPLIT_STR function from query
SET #ledger = "Cash,Special Offer";
CREATE TEMPORARY TABLE IF NOT EXISTS `selected_types` (type varchar(50));
#inserting splitted values to temp table
simple_loop: LOOP
SET indx=indx+1;
SET str=SPLIT_STR(x_id_list,',',indx);
IF str='' THEN
LEAVE simple_loop;
END IF;
INSERT INTO selected_types VALUES(str);
END LOOP simple_loop;
#filter with temp table
SELECT `_ledger` FROM
`acc_ledger` led
inner join selected_types tmp on tmp.type = led._ledger;

How to create an empty view (or array) and later insert data in it?

This is because I am creating a procedure which generates items (by calling another function) which then finally needs to return me an array sort of collection including all the values.
I am thinking I can do it with a temporary table or View.
DROP PROCEDURE IF EXISTS getAllStrings $$
CREATE PROCEDURE getAllStrings(IN idString varchar(256), OUT finalSeparate VARCHAR(256))
BEGIN
SELECT LENGTH(idString) - LENGTH(REPLACE(idString, ',', '')) INTO #noOfCommas;
IF #noOfCommas = 0
THEN
SELECT idString;
ELSE
CREATE OR REPLACE VIEW v AS SELECT NULL AS SPLIT_VALUES WHERE FALSE;
SET #y = 1;
WHILE #y <= #noOfCommas DO
SELECT splitString(idString, ', ', #y) INTO #tempSeparate;
SET #y = #y + 1;
INSERT INTO v (SPLIT_VALUES) values(#tempSeparate);
END WHILE;
SELECT SPLIT_VALUES FROM v;
END IF;
END $$
It says that
Target table v is not insertable-into
How do I do it? I even tried changing the algorithm to merge but it didn't work.
The one with a table works, but I am looking for a data structure that can hold it better, maybe #json, I don't know.
DELIMITER $$
DROP FUNCTION IF EXISTS splitString $$
CREATE FUNCTION splitString (
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255) deterministic
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '') $$
DROP PROCEDURE IF EXISTS getAllStrings $$
CREATE PROCEDURE getAllStrings(IN idString varchar(256), OUT finalSeparate VARCHAR(256))
BEGIN
SELECT LENGTH(idString) - LENGTH(REPLACE(idString, ', ', '')) INTO #noOfCommas;
IF #noOfCommas = 0
THEN
SELECT idString;
ELSE
DROP TABLE IF EXISTS TEMP_SEP_STRINGS;
CREATE TABLE IF NOT EXISTS TEMP_SEP_STRINGS( SPLIT_VALUES VARCHAR(256));
SET #y = 1;
WHILE #y <= #noOfCommas DO
SELECT splitString(idString, ', ', #y) INTO #tempSeparate;
SET #y = #y + 1;
INSERT INTO TEMP_SEP_STRINGS(SPLIT_VALUES) values(#tempSeparate);
END WHILE;
SELECT * FROM TEMP_SEP_STRINGS;
END IF;
END $$
CALL getAllStrings('b, a', #multiple);
A temporary table has been the best option for me yet. It gets deallocated when the session ends or the connection is closed (although you can drop it before that happens) and just gobbles up whatever data you give it.
No need to define its columns and stuff and you can probably use it anywhere via SELECT command such as with the SQL IN operator.
An example:
CREATE TEMPORARY TABLE temp_table
SELECT tree_id FROM TREES WHERE fruit_type='Apple';

How to only with SQL select column form table A, split its by separator and insert separated values to other table?

sorry for my english :)
I need (only with SQL) select all column values form table A, split its by separator and insert separated values to other table?
For example, i have table like this:
ID, NAME, MAGIC
1, Marty, ACD ACFX U128BH
and i need export "MAGIC" value to separate table like this:
ID, USERID, MAGIC
1, 1, ACD
2, 1, ACFX
3, 1, U128BH
How to do it? I found eg. SQL insert data to other table after split string, but this is MS SQL syntax (?) and im using MySQL.
Thanx for reply
EDIT THIS ANSWER TO MUCH YOUR WORK
#drop table magic_t;
create table magic_t(
id int,
name varchar(100),
magic varchar(100)
);
#drop table split_insert;
create table split_insert(
split_value varchar(100)
);
insert into magic_t values(1,'nam1','VAL1 VAL2 VAL3');
insert into magic_t values(1,'nam1','VAL4 VAL5 VAL3');
#drop function strSplit;
DELIMITER ;;
CREATE FUNCTION strSplit(x varchar(255), delim varchar(12), pos int) returns varchar(255)
return replace(substring(substring_index(x, delim, pos), length(substring_index(x, delim, pos - 1)) + 1), delim, '');;
DELIMITER ;
#drop procedure split_table;
DELIMITER ;;
CREATE procedure split_table ()
begin
DECLARE done INT DEFAULT 0;
DECLARE while_condition boolean default true;
DECLARE counter INT DEFAULT 0;
DECLARE magic_value varchar(100);
declare splited_value varchar(100);
DECLARE colum_list CURSOR FOR SELECT magic FROM magic_t;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; -- to stop loop when fetch is over
OPEN colum_list;
REPEAT FETCH colum_list INTO magic_value;
if done =0 then
set counter =1;
set while_condition = true;
while(while_condition) do
set splited_value = strSplit(magic_value,' ',counter) ;
if splited_value <>'' then
insert into split_insert values(splited_value);
end if;
if splited_value='' then set while_condition=false; end if;
set counter =counter+1;
end while;
end if;
UNTIL done -- here stop repeat when done =1
END REPEAT;
end ;;
DELIMITER ;
call split_table ();
select * from split_insert;

MySQL stored procedure insert multiple rows from list

How can I write a MySQL stored procedure to insert values from a variable sized list? More specifically I need to insert data into one parent table, get the ID from the insert, and then insert a variable number of child records along with the new ID into another table in a one-to-many relationship. My schema looks something like this:
TableA:
table_a_id -- Auto Increment
counter
some_data...
TableB:
table_b_id -- Auto Increment
table_a_id -- Foreign Key Constraint
some_data_from_list...
My stored procedure so far looks like this:
DELIMITER ;;
CREATE PROCEDURE insert_group_alert(
IN _some_data_a VARCHAR(255),
IN _data_list_b TEXT,
)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
END;
START TRANSACTION;
INSERT INTO TableA (
some_data,
counter
)
VALUES (
_some_data_a,
1
)
ON DUPLICATE KEY UPDATE
counter = counter + 1;
SELECT last_insert_id()
INTO #newId;
LIST INSERT ???:
INSERT INTO TableB (
table_a_id, some_data
) VALUES (
#newId,
list_item,
);
END LIST INSERT ???
COMMIT;
END ;;
DELIMITER ;
My thought was to pass in a list of items to insert into table B via a comma delimited string. The values are strings. I am not sure what to do in the LIST INSERT section. Do I need a loop of some sort? Is this stored procedure I have so far the correct way to do this? I don't want to do a batch as I could potentially have hundreds or even thousands of items in the list. Is there a better solution? I am using straight JDBC.
Yes, you need a loop, in which you can use substring_index() to get the values within the list. The solution is based on the answers from this SO topic:
DELIMITER ;;
CREATE PROCEDURE insert_group_alert(
IN _some_data_a VARCHAR(255),
IN _data_list_b TEXT,
)
BEGIN
DECLARE strLen INT DEFAULT 0;
DECLARE SubStrLen INT DEFAULT 0;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
END;
START TRANSACTION;
INSERT INTO TableA (
some_data,
counter
)
VALUES (
_some_data_a,
1
) -- you do not really need this, since you do not provide an id
ON DUPLICATE KEY UPDATE
counter = counter + 1;
SELECT last_insert_id()
INTO #newId;
do_this:
LOOP
SET strLen = CHAR_LENGTH(_data_list_b);
INSERT INTO TableB (table_a_id, some_data) VALUES(#newId,SUBSTRING_INDEX(_data_list_b, ',', 1));
SET SubStrLen = CHAR_LENGTH(SUBSTRING_INDEX(_data_list_b, ',', 1))+2;
SET _data_list_b = MID(_data_list_b, SubStrLen, strLen); --cut the 1st list item out
IF _data_list_b = '' THEN
LEAVE do_this;
END IF;
END LOOP do_this;
COMMIT;
END ;;
DELIMITER ;