I want to do a Migration data from one table to another.
I wrote a simple PHP script for my purposes but I wanted to do this by MySql script using user defined variables:
the PHP script looks like this:
//MIGRATION
$sql = "SELECT position FROM ts_user_config WHERE position != '' AND position NOT REGEXP '^-?[0-9]+$' GROUP BY TRIM(position) ORDER BY position";
$positions = db_loadColumn( $sql );
foreach ($positions as $key => $pos) {
$sql = "SELECT id FROM user_positions where UPPER(position) = UPPER('$pos')";
$posId = db_loadResult($sql);
if ($posId == null) {
$sql = "INSERT INTO user_positions (position, `desc`) VALUES ('$pos', '$pos')";
db_exec($sql);
$posId = db_insert_id();
}
$sql = "UPDATE ts_user_config SET position='$posId' WHERE TRIM(position)='$pos'";
db_exec($sql);
}
//---------
Could somebody be so kind and rewrite this PHP instructions to MySQL script? I tried to do this but my mySQL knowledge is very low and I couldn't done that.
Please help me if its not too much effort.
Thank you in advance.
I Have DONE IT !!!!!! :)
this is my mySQL script, I dont know if its perfect but its do what I need. Please tell me if I can make something better here. Thanks again :)
drop procedure if exists PositionMigration;
delimiter '//'
CREATE PROCEDURE PositionMigration()
BEGIN
BLOCK1: BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE pos VARCHAR(100);
DECLARE posId1 INT;
DECLARE posId2 INT;
DECLARE sql1 CURSOR FOR SELECT position FROM ts_user_config WHERE position != '' AND position NOT REGEXP '^-?[0-9]+$' GROUP BY TRIM(position) ORDER BY position;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN sql1;
read_loop: LOOP
FETCH sql1 INTO pos;
IF done THEN
LEAVE read_loop;
END IF;
BLOCK2: BEGIN
DECLARE posNotFound INT DEFAULT FALSE;
DECLARE sql2 CURSOR FOR SELECT id FROM user_positions where UPPER(position) = UPPER(pos);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET posNotFound = TRUE;
OPEN sql2;
FETCH sql2 INTO posId1;
IF posNotFound THEN
INSERT INTO user_positions (position, \`desc\`) VALUES (pos, pos);
BLOCK3: BEGIN
DECLARE sql3 CURSOR FOR SELECT LAST_INSERT_ID();
OPEN sql3;
FETCH sql3 INTO posId2;
UPDATE ts_user_config SET position=posId2 WHERE TRIM(position)=pos;
CLOSE sql3;
END BLOCK3;
ELSE
UPDATE ts_user_config SET position=posId1 WHERE TRIM(position)=pos;
END IF;
CLOSE sql2;
END BLOCK2;
END LOOP;
CLOSE sql1;
END BLOCK1;
END;
//
delimiter ';'
call PositionMigration();
Related
I have written a stored procedure in mysql which will create a TEMPORARY TABLE, I want to access the data of TEMPORARY TABLE using Codeigniter.But when I call "$this->db->query()" it returns empty data.
$data=array();
$call_procedure = "CALL sp_Stock()";
$query = $this->db->query($call_procedure);
$sql="SELECT * FROM StockTable";
$query1 = $this->db->query($sql);
I have changed my way to show the data. And I do changes it on stored procedure.
DELIMITER $$
CREATE PROCEDURE sp_Stock()
BEGIN
DECLARE cursor_finish INTEGER DEFAULT 0;
DECLARE m_numofpurchaseBag DECIMAL(10,2)DEFAULT 0;
DECLARE m_purchasedKg DECIMAL(10,2)DEFAULT 0;
DECLARE m_purBagDtlId INTEGER DEFAULT 0;
DECLARE stockCursor CURSOR FOR
SELECT purchase_bag_details.`actual_bags`,
(purchase_bag_details.`net`*purchase_bag_details.`actual_bags`) AS PurchasedKg,
purchase_bag_details.`id` AS PurchaseBagDtlId
FROM
purchase_invoice_detail
INNER JOIN
purchase_bag_details
ON purchase_invoice_detail.`id`= purchase_bag_details.`purchasedtlid`
INNER JOIN
`do_to_transporter`
ON purchase_invoice_detail.`id` = do_to_transporter.`purchase_inv_dtlid`
WHERE purchase_invoice_detail.`teagroup_master_id`=6
AND purchase_invoice_detail.`id`=1481
AND do_to_transporter.`in_Stock`='Y';
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET cursor_finish = 1;
DROP TEMPORARY TABLE IF EXISTS StockTable;
#temptable creation
CREATE TEMPORARY TABLE IF NOT EXISTS StockTable
(
purchaseBagDtlId INT,
purchasedBag NUMERIC(10,2),
purchasedKg NUMERIC(10,2),
blendedBag NUMERIC(10,2),
blendedKg NUMERIC(10,2),
stockBag NUMERIC(10,2),
stockKg NUMERIC(10,2)
);
#temptable creation
OPEN stockCursor ;
get_stock : LOOP
FETCH stockCursor INTO m_numofpurchaseBag,m_purchasedKg,m_purBagDtlId;
IF cursor_finish = 1 THEN
LEAVE get_stock;
END IF;
/*SELECT m_numofpurchaseBag,m_purchasedKg,m_purBagDtlId; */
/* Blending bag query*/
SET #m_numberofBlndBag:=0;
SET #m_BlndKg:=0;
/* Blend bag*/
SELECT #m_numberofBlndBag:=SUM(blending_details.`number_of_blended_bag`) AS belendedBag INTO #m_numberofBlndBag
FROM blending_details
WHERE blending_details.`purchasebag_id`= m_purBagDtlId
GROUP BY
blending_details.`purchasebag_id`;
#Blend Bag
#Blend Kgs
SELECT #m_BlndKg:=SUM(blending_details.`qty_of_bag` * blending_details.`number_of_blended_bag`) AS blendkg INTO #m_BlndKg
FROM blending_details
WHERE blending_details.`purchasebag_id`= m_purBagDtlId
GROUP BY
blending_details.`purchasebag_id`;
SET #m_StockBag:=(m_numofpurchaseBag - #m_numberofBlndBag);
SET #m_StockKg:=(m_purchasedKg - #m_BlndKg);
INSERT INTO StockTable
(
purchaseBagDtlId ,
purchasedBag ,
purchasedKg ,
blendedBag ,
blendedKg ,
stockBag ,
stockKg
)VALUES(m_purBagDtlId,m_numofpurchaseBag,m_purchasedKg,#m_numberofBlndBag,#m_BlndKg,#m_StockBag,#m_StockKg);
END LOOP get_stock;
CLOSE stockCursor;
SELECT * FROM StockTable;
#DROP TABLE StockTable;
END$$
DELIMITER ;
#CALL sp_Stock();
Anywhere I am using a temp table I am executing this before the temp table is created:
$this->db->query('DROP TABLE IF EXISTS StockTable');
I seem to remember reading someone having the same problem as you and for some reason you have to execute the above first.
So try:
$data=array();
$call_procedure = "CALL sp_Stock()";
$this->db->query('DROP TABLE IF EXISTS StockTable');
$query = $this->db->query($call_procedure);
$sql="SELECT * FROM StockTable";
$query1 = $this->db->query($sql);
I have table name "tmp1" which has fields name id and comma. comma contains 1,3,2,4,5 values with text datatype.
I want separate value in loop in my stored procedure. Like 1 then 3 then 2 etc. So, I was create below given procedure.
BEGIN
DECLARE my_delimiter CHAR(1);
DECLARE split_comma text;
DECLARE done INT;
DECLARE occurance INT;
DECLARE i INT;
DECLARE id INT;
DECLARE sel_query VARCHAR(500);
DECLARE splitter_cur CURSOR FOR SELECT id,comma from tmp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
OPEN splitter_cur;
splitter_loop:LOOP
FETCH splitter_cur INTO id,split_comma;
SET occurance = LENGTH(split_comma) - LENGTH(REPLACE(split_comma,',',''))+1;
SET my_delimiter=',';
IF done = 1 THEN
LEAVE splitter_loop;
END IF;
IF occurance > 0 THEN
SET i = 1;
WHILE i <= occurance DO
SET sel_query = "SELECT SUBSTRING_INDEX(comma,',',i) as abc from tmp1";
SET #sel_query = sel_query;
PREPARE sel_query FROM #sel_query;
EXECUTE sel_query;
SET i = i + 1;
END WHILE;
END IF;
SET occurance = 0;
END LOOP;
CLOSE splitter_cur;
END;
But when I execute this procedure error occurs "MySQL said: #1054 - Unknown column 'i' in 'field list'"
But here i is a variable used to rotate loop.
Is there any solution? Please help me ... Thank you in advance ...
You have to embed value of variable i in the query, which you did not do.
Change:
SET sel_query = "SELECT SUBSTRING_INDEX(comma,',',i) as abc from tmp1";
SET #sel_query = sel_query;
To:
SET #sel_query = CONCAT( "SELECT SUBSTRING_INDEX(comma,',',", i, ") as abc from tmp1 )";
I have created the following stored proc in MySQL:
BEGIN
DECLARE temp_mold VARCHAR(30);
DECLARE temp_time DATETIME;
DECLARE temp_shot_ct INT;
DECLARE temp_shot_count INT;
DECLARE temp_shot_mold VARCHAR(30);
DECLARE temp_shot_cavity_count INT;
DECLARE temp_job_count INT;
DECLARE temp_row_count INT;
DECLARE done INT DEFAULT 0;
DECLARE temp_shot_counter INT DEFAULT 1;
DECLARE setup_time INT;
DECLARE cursor1 CURSOR FOR
SELECT
(schedule_run_time*3600)/(schedule_qty/schedule_cavity_count) as shot_ct,
ROUND((schedule_qty/schedule_cavity_count)) as shot_count,
schedule_mold as shot_mold, schedule_cavity_count as shot_cavity_count
FROM `schedule`
WHERE `schedule`.schedule_cell = start_cell AND `schedule`.schedule_date = DATE(start_time) AND deleted = 0
ORDER BY schedule_order ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
SELECT COUNT(schedule_id) INTO temp_job_count FROM `schedule` WHERE schedule_cell = start_cell AND schedule_date = DATE(start_time) AND deleted = 0;
SELECT cell_setup_standard INTO setup_time FROM cells WHERE cell_name = start_cell;
DROP TEMPORARY TABLE IF EXISTS rst;
CREATE TEMPORARY TABLE rst (
shot_id INT NOT NULL AUTO_INCREMENT,
shot_datetime datetime,
shot_qty INT,
PRIMARY KEY (shot_id)
);
SET temp_time = start_time;
SET temp_mold = "";
SET temp_row_count = 0;
INSERT INTO rst (shot_datetime) VALUES (temp_time);
#Loop through cursor1 recordset
OPEN cursor1;
start_loop: LOOP
FETCH cursor1 INTO temp_shot_ct, temp_shot_count, temp_shot_mold, temp_shot_cavity_count;
IF done THEN
LEAVE start_loop;
END IF;
IF temp_mold <> temp_shot_mold THEN
SET temp_time = TIMESTAMPADD(MINUTE,setup_time,temp_time); #Add standard setup time whenever the mold changes
INSERT INTO rst (shot_datetime) VALUES (temp_time);
SET temp_mold = temp_shot_mold;
END IF;
SET temp_shot_counter = 1;
REPEAT
SET temp_time = TIMESTAMPADD(SECOND,temp_shot_ct,temp_time);
INSERT INTO rst (shot_datetime,shot_qty) VALUES (temp_time,temp_shot_cavity_count);
SET temp_shot_counter = temp_shot_counter + 1;
UNTIL temp_shot_counter > temp_shot_count
END REPEAT;
END LOOP;
CLOSE cursor1;
#Return records. Will be used in ORM to loop through records on the PHP side
SELECT SUM(shot_qty) AS qty, HOUR(shot_datetime) AS shot_hour FROM rst GROUP BY HOUR(shot_datetime);
DROP TEMPORARY TABLE IF EXISTS rst;
END
I'm not the greatest at using stored procedures. This one is running very slow - it takes about 6 seconds to run. The queries individually run in milliseconds, so I was expecting this to take 100 milliseconds at most. I can't figure out what I'm doing wrong that's causing this to run so slowly. Does anyone have any ideas on how to make this run quicker? I can create a PHP script to do the same thing, but I'm trying to get better at using stored procs and functions in MySQL - and this is far less code. I welcome any and all advice.
trying to create a stored procedure through mysqli in php. Keep throwing back at 1064 error when I do it through php, but is fine, when I copy the same sql command in php. Anyone able to shed some light on what could be the problem, or anyone found a solution to something similar?
Create Procedure code:
DELIMITER //
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN taskID INT, IN avaliableTime INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a INT;
DECLARE cur1 CURSOR FOR SELECT t.`task_id`
FROM `task_dependency` d
LEFT OUTER JOIN (
SELECT `task_id`, `dependent_on_task_id`, ti.`id`, min(ti.`completed`) as make_live
FROM `task_dependency`
LEFT OUTER JOIN (
SELECT `task_id`, `completed` FROM `6de443aacf727f6009e857480f33153c_tasks`
) ti ON ti.`task_id` = `dependent_on_task_id`
GROUP BY `task_id`
) t ON d.`task_id` = t.`task_id`
WHERE d.`dependent_on_task_id` = taskID
AND t.make_live > 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `live` = 1 WHERE `task_id` = a;
END LOOP;
CLOSE cur1;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `earliest_start` = avaliableTime WHERE `task_id` IN (
SELECT `task_id`
FROM `task_dependency`
WHERE `dependent_on_task_id` = taskID
) AND `earliest_start` < avaliableTime;
END //
DELIMITER ;
PHP Code:
if (!$this->dbc->query("DROP PROCEDURE IF EXISTS " . $this->table_prefix . "makeLive") ||
!$this->dbc->query($query)) {
echo "Stored procedure creation failed: (" . $this->dbc->errno . ") " . $this->dbc->error . "<br /><br />" . $query;
exit;
}
echo "success";
exit;
Script execution is throwing out the following error...
Stored procedure creation failed: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER //
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN tas' at line 1
Although it says it's a syntax error, the fact a straight copy and paste of the command to phpmyadmin seems to work, looks like it's gotta be some sort of mysqli bug perhaps?
The query function only allows you to execute a single query. Try the multi_query function (documentation: http://www.php.net/manual/en/mysqli.multi-query.php ) instead. It allows for multiple commands, and may fix your issue.
Turns out mysqli->query already parses semi-colons and runs the whole command. The delimiter commands at the start and end of the query where unnecessary in successfully creating the procedure.
Just remove DELIMITER and it will work. So in your case it will be like this:
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN taskID INT, IN avaliableTime INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a INT;
DECLARE cur1 CURSOR FOR SELECT t.`task_id`
FROM `task_dependency` d
LEFT OUTER JOIN (
SELECT `task_id`, `dependent_on_task_id`, ti.`id`, min(ti.`completed`) as make_live
FROM `task_dependency`
LEFT OUTER JOIN (
SELECT `task_id`, `completed` FROM `6de443aacf727f6009e857480f33153c_tasks`
) ti ON ti.`task_id` = `dependent_on_task_id`
GROUP BY `task_id`
) t ON d.`task_id` = t.`task_id`
WHERE d.`dependent_on_task_id` = taskID
AND t.make_live > 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `live` = 1 WHERE `task_id` = a;
END LOOP;
CLOSE cur1;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `earliest_start` = avaliableTime WHERE `task_id` IN (
SELECT `task_id`
FROM `task_dependency`
WHERE `dependent_on_task_id` = taskID
) AND `earliest_start` < avaliableTime;
END;
hi i am using mysql trigger to update a table on another table's insertion
this trigger works fine
CREATE TRIGGER `update_pupil_subject` AFTER INSERT ON `pupil_marks`
FOR EACH ROW
BEGIN
UPDATE pupil_subjects SET NumberOfStudens = NumberOfStudens + 1 WHERE NEW.SubjectID = SubjectID;
END$$
but this gives an error
CREATE TRIGGER `update_pupil_subject` AFTER INSERT ON `pupil_marks`
FOR EACH ROW
BEGIN
UPDATE pupil_subjects SET NumberOfStudens = NumberOfStudens + 1 , AverageMarks = (SELECT AVG(Marks) FROM pupil_marks WHERE NEW.StudentID = StudentID ) WHERE NEW.SubjectID = SubjectID;
END$$
how to write this correctly , please help . thanks in advance .
Apparently there were problems when sub-queries were used:
Can you try splitting the SQL statement:
DELIMITER $$
CREATE TRIGGER `update_pupil_subject`
AFTER INSERT
ON `pupil_marks`
FOR EACH ROW
BEGIN
DECLARE avg_marks float;
SELECT AVG(Marks)
INTO avg_marks
FROM pupil_marks
WHERE NEW.SubjectID = SubjectID;
UPDATE pupil_subjects
SET NumberOfStudens = NumberOfStudens + 1, AverageMarks = avg_marks
WHERE NEW.SubjectID = SubjectID;
END
$$
Edit: Use
SHOW TRIGGERS WHERE `table` = 'pupil_marks';
to get all triggers defined on pupil_marks. You can't have multiple triggers on an event as all actions can be covered in single trigger.
NOTE: I think AVG(Marks) is for a given subject, so modified trigger definition accordingly.
declare a variable inside the trigger and assign it with the subquery
declare avg_mark integer default 0;
set avg_mark := (SELECT AVG(Marks) FROM pupil_marks WHERE NEW.StudentID = StudentID);
then use the variable "avg_mark" in your update statement...
it may work...
if not then check the delimiter just below phpmyadmin sql box . It should be "$$"