marcoullis
asked on
MySQL procedure to create a table each time another procedure is called (merging two scripts)
The attached code is a MySQL procedure. It fills the row of one table (node_) with rows of another table (master_) based on a series of conditions. Here is the creation script for node_ table:
SELECT @tblname:=concat('node_',d ate_format (now(),'%Y %m%d%H%i%S '),'');
SELECT @cr := concat('CREATE TABLE ',@tblname,' (
`node_id` INT(5),
`node_code` VARCHAR(4),
`node_1st` VARCHAR(1),
`node_2nd` VARCHAR(1),
`node_3rd` VARCHAR(2),
`node_act` VARCHAR(1),
`node_ref` INT(4),
`node_dur` INT(4),
`node_suc` INT(4),
`node_log` VARCHAR(2),
`node_lag` INT(4),
`node_con` INT(4))','');
PREPARE stmt from @cr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Now what i need to know how to do is how to have the attached code incorporate the above creation scipt, in such that each time the attached code is called, a new table is created.
So how do I do that?
SELECT @tblname:=concat('node_',d
SELECT @cr := concat('CREATE TABLE ',@tblname,' (
`node_id` INT(5),
`node_code` VARCHAR(4),
`node_1st` VARCHAR(1),
`node_2nd` VARCHAR(1),
`node_3rd` VARCHAR(2),
`node_act` VARCHAR(1),
`node_ref` INT(4),
`node_dur` INT(4),
`node_suc` INT(4),
`node_log` VARCHAR(2),
`node_lag` INT(4),
`node_con` INT(4))','');
PREPARE stmt from @cr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Now what i need to know how to do is how to have the attached code incorporate the above creation scipt, in such that each time the attached code is called, a new table is created.
So how do I do that?
create procedure WALKTHROUGH()
BEGIN
DECLARE rowwalker CURSOR as select * from "master_" order by primary_key ASC;
OPEN rowwalker;
read_loop: LOOP
WHILE rowwalker.PRE iS not null DO
fetch rowwalker;
IF done THEN
LEAVE read_loop;
END IF;
end while;
INSERT INTO NODE_ (node_act, node_ref, node_suc, node_log, node_lag, node_con)
VALUES ('1', rowwalker.REF, rowwalker.SUC, rowwalker.LOG, rowwalker.LAG, rowwalker.CON);
INSERT INTO NODE_ (node_act, node_ref, node_suc, node_log, node_lag, node_con)
VALUES (SELECT '1', REF, SUC, LOG, LAG, CON FROM master_ WHERE SUC = rowwalker.SUC);
IF done THEN
LEAVE read_loop;
END IF;
END LOOP;
CLOSE rowwalker;
END;
ASKER
or is that not possible?
You could place it at the top of the procedure right after begin and replace the reference to NODE_ with the variable @tblname.
Depending on the frequency with which this procedure runs, a check to see whether the table already exists as to avoid issuing create table when the table already exists
add if not exist
create table if not exists @tblname
Presumably this is what caused the SP to abort when the creation was included due to creation table error.
Depending on the frequency with which this procedure runs, a check to see whether the table already exists as to avoid issuing create table when the table already exists
add if not exist
create table if not exists @tblname
Presumably this is what caused the SP to abort when the creation was included due to creation table error.
ASKER
could you suggest the correct code pls?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK i will accept the solution because a real effort was made, but it didn't work when i tried to implement it... but i don't know why. will open a new question to filter out the reason it didn't work.
ASKER
A solid effort, but on implementation in Toad for MySQL it did not work. Will proceed to open a new question to filter out the problem.
ASKER
how can we make the procedure know to apply the attached code to the right table?