So I'm trying to work out a trigger that automatically adds the ingredients from a order (dhh_purchaseorder) to the table dhh_ingredients. It should get the new ordered amount out of dhh_purchaseorderingredient and dump it into the orderAmount variable. Then it should combine the current available stock with the ordered amount in newStock. Then it should update the ingredient table and set the new amounts for the correct ingredients but for some reason it keeps putting the currentStock column in ingredients to 0.
Here is my trigger:
BEGIN
DECLARE orderStatus, orderIngredientName VARCHAR(50);
DECLARE finished INTEGER DEFAULT 0;
DECLARE currentStock, newStock DECIMAL(10,2);
DECLARE orderNo, orderAmount int(10);
DECLARE lookupCursor CURSOR FOR SELECT INGREDIENTingredientName from dhh_purchaseorderingredient WHERE dhh_purchaseorderingredient.PURCHASEpurchaseOrderNo = NEW.purchaseOrderNo;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
SET orderNo = NEW.purchaseOrderNo;
SET orderStatus = NEW.status;
IF(orderStatus = "DELIVERED") THEN
OPEN lookupCursor;
update_loop: LOOP
FETCH lookupCursor INTO orderIngredientName;
IF finished = 1 THEN
LEAVE update_loop;
END IF;
SET orderAmount = (SELECT amount from dhh_purchaseorderingredient WHERE dhh_purchaseorderingredient.PURCHASEpurchaseOrderNo = orderNo AND dhh_purchaseorderingredient.INGREDIENTingredientName = orderIngredientName);
SET currentStock = (SELECT currentStock FROM dhh_ingredient WHERE ingredientName = orderIngredientName);
SET newStock = currentStock + orderAmount;
INSERT INTO temp VALUES(currentStock);
UPDATE dhh_ingredient
SET currentStock = newStock
WHERE ingredientName = ingredientName;
END LOOP update_loop;
CLOSE lookupCursor;
END IF;
END
This is partly an answer and partly a suggestion for a better approach.
One of the many reasons I believe stored procedures are a very bad idea is that they are hard to debug. In this case, it's not easy to figure out what value is being assigned to the various stock amount variables.
A better approach is to use query that does update - without the need for SP variable. It's really easy to test it; just execute and inspect the results. By using such a query in your SP, you eliminate incorrect query logic from your code, allowing you to condense the SP to just the bits that matter - the update.
Fortunately, MySQL provides a way to update through a join, via its multi-table update syntax.
This is what the query could look like:
UPDATE dhh_ingredient, dhh_purchaseorderingredient
SET = currentStock = currentStock - amount
WHERE dhh_purchaseorderingredient.INGREDIENTingredientName = orderIngredientName
AND ingredientName = orderIngredientName
AND dhh_purchaseorderingredient.PURCHASEpurchaseOrderNo = NEW.purchaseOrderNo
AND NEW.status = 'DELIVERED';
No need for local variables or any loops, because the update query does a join and the NEW values are used directly in the query.
So your entire stored procedure becomes simply:
BEGIN
UPDATE dhh_ingredient, dhh_purchaseorderingredient
SET = currentStock = currentStock - amount
WHERE dhh_purchaseorderingredient.INGREDIENTingredientName = orderIngredientName
AND ingredientName = orderIngredientName
AND dhh_purchaseorderingredient.PURCHASEpurchaseOrderNo = NEW.purchaseOrderNo
AND NEW.status = 'DELIVERED';
END
which is easy to understand, maintain and debug - a better solution.
Related
I'll admit, up front, that this is my first MySQL trigger. The MySQL version is 8.0.
I have a table for orders and order line items. In the orders table I have a column that stores the total of all line items. I need a trigger that updates that total every time a line items is added or changed for that order. Here's what I have so far:
CREATE TRIGGER update_line_item_total
AFTER INSERT
ON orderlines.price FOR EACH ROW
BEGIN
-- variable declarations
DECLARE li_order_id INT;
DECLARE li_total DEC;
SET li_order_id = orderlines.order_id;
SET li_total = SUM (orderlines.price) WHERE orderlines.order_id = li_order_id;
-- trigger code
UPDATE orders SET
total_lineitems = li_total
WHERE orders.order_id = li_order_id;
END;
I put this into MySQL Workbench and it was rejected. I know that the DECLARE statements, the 2nd SET statement, and the END statements were the ones flagged as incorrect, however I'm not even sure where to look to find out what's wrong.
Looking for guidance and direction.
Thanks,
Michael Frankel
Wizard Consulting Group, Inc.
your problem is on this line : SET li_total = SUM (orderlines.price) WHERE orderlines.order_id = li_order_id;
add delimiters.
do this :
delimiter |
CREATE TRIGGER update_line_item_total
AFTER INSERT
ON orderlines.price FOR EACH ROW
BEGIN
-- variable declarations
DECLARE li_order_id INT;
DECLARE li_total DEC;
declare li_total_temp dec;
SET li_order_id = orderlines.order_id;
select orderlines.price into li_total_temp WHERE orderlines.order_id = li_order_id;
SET li_total = li_total + li_total_temp;
-- trigger code
UPDATE orders SET
total_lineitems = li_total
WHERE orders.order_id = li_order_id;
END |
delimiter ;
I have 3 tables
tbl_payments(pay_id,date,amount,description,-------)
tbl_pay_trans(pay_id,trans_id)
tbl_transactions(trans_id,trans_date,trans_amount,trans_description,-----)
'tbl_transaction' has the same data from the 'tbl_payments' along with some other values. To maintain the relationship between the two tables I use 'tbl_pay_trans'. What I want to do is, when an update done on tbl_payments(amount,description) the same changes need to do in tbl_transactions(trans_amount,trans_description). I wrote a trigger to do that, but it dose not update the tbl_transaction table values as it supposed to.
My trigger is
DELIMITER $$
CREATE TRIGGER update_trans
AFTER UPDATE on tbl_payments
FOR EACH ROW
BEGIN
DECLARE new_amount VARCHAR(50);
DECLARE new_description TEXT;
DECLARE new_pay_id,new_trans_id INT;
SET new_pay_id = OLD.pay_id;
SET new_amount = OLD.amount;
SET new_description = OLD.description;
SELECT trans_id INTO new_trans_id FROM tbl_pay_trans WHERE pay_id = new_pay_id;
UPDATE tbl_transactions SET
trans_amount = new_amount,
trans_description = new_description
WHERE trans_id = new_trans_id;
END$$
DELIMITER ;
Please someone help me to figure out what I did wrong.
It's not updating because you are using OLD on the amount and description columns where you need to use NEW i.e.
SET new_amount = NEW.amount;
SET new_description = NEW.description;
OLD refers to the column value before the update occurred in tbl_payments. See the manual.
I searched a lot of doing a task but found no appropriate solution.
Basically the scenario is. I have a user_comment table in which there are 5 column(id,parent_id,user_comments,is_deleted,modified_datetime). There is a parent child relationship like 1->2,1->3,2->4,2->5,5->7 etc. Now i am sending the id from the front end and i want to update the column is_deleted to 1 and modified_datetime on all the records on
this id as well as the all the children and children's of children.
I am trying to doing this by using a recursive procedure. Below is the code of my procedure
CREATE DEFINER=`root`#`localhost` PROCEDURE `user_comments`(
IN mode varchar(45),
IN comment_id int,
)
BEGIN
DECLARE p_id INT DEFAULT NULL ;
if(mode = 'delete')
then
update user_comment set is_deleted = 1, modified_datetime = now()
where id = comment_id ;
select id from user_comment where parent_id = comment_id into p_id ;
if p_id is not null
then
SET ##GLOBAL.max_sp_recursion_depth = 255;
SET ##session.max_sp_recursion_depth = 255;
call user_comments('delete', p_id);
end if;
end if;
END
By using this procedure it give me an error of more than one row.
If i return the select query without giving it to variable then shows me the the appropriate results on the select query but i have to call this procedure recursively based on getting the ids of the select query.
I need help i have already passed 2 days into this.
I used cursor also. Below is the code of cursor
CREATE DEFINER=`root`#`localhost` PROCEDURE `user_comments`(
IN mode varchar(45),
IN comment_id int,
)
BEGIN
DECLARE p_emp int;
DECLARE noMoreRow INT;
DECLARE cur_emp CURSOR FOR select id from user_comment where parent_id = comment_id ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET noMoreRow = 0;
if(mode = 'delete')
then
OPEN cur_emp;
LOOPROWS: LOOP
IF noMoreRow = 0 THEN
update user_comment set is_deleted = 1, modified_datetime = now() where id = comment_id
CLOSE cur_emp;
LEAVE LOOPROWS;
END IF;
FETCH cur_emp INTO p_emp;
update user_comment set is_deleted = 1, modified_datetime = now() where id = p_emp ;
SET ##GLOBAL.max_sp_recursion_depth = 255;
SET ##session.max_sp_recursion_depth = 255;
call user_comments('delete', p_emp);
END LOOP;
end if;
END
After using cursor i am getting a thread error.i don't know how can overcome this problem!!!
Mysql's documentation on select ... into varlist clearly says:
The selected values are assigned to the variables. The number of
variables must match the number of columns. The query should return a
single row. If the query returns no rows, a warning with error code
1329 occurs (No data), and the variable values remain unchanged. If
the query returns multiple rows, error 1172 occurs (Result consisted
of more than one row). If it is possible that the statement may
retrieve multiple rows, you can use LIMIT 1 to limit the result set to
a single row.
Since you wrote in the OP that a comment can be parent of many comments, using simple variables cannot be a solution. You should use a CURSOR instead, that can store an entire resultset.
You loop through the records within the cursos as shown in the sample code in the above link and call user_comments() in a recursive way.
UPDATE
If your receive
Error Code: 1436. Thread stack overrun
error, then you can do 2 things:
Increase the thread_stack setting in the config file and restart mysql server.
You can try to simplify your code to use less recursions and therefore less stack space. For example, when you fetch all children into the cursor, then rather calling the user_comments() recursively for each, you can set all direct children's status within the code and call the function recirsively on grand-childrens only (if any). You can also change your data structure and use nested set model to approach hierarchical structures.
Nested set model is more complex to understand, it is less resource intensive to traverse, but more resource intensive to maintain.
Im just trying using stored procedure in mysql. i tried to create a table name "Book". i have 5 columns before. they are idBook, bookName, bookYear, and Stock. So, i added 2 columns for rating and review. and i wanted to give those column with value using looping in stored procedure. and this is my code :
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getBookExcitement`()
BEGIN
DECLARE bookRating INT DEFAULT 0;
DECLARE count INT DEFAULT 0;
DECLARE bookReview VARCHAR (100);
DECLARE sums INT;
SELECT count(idBook) INTO sums FROM Book;
getBookExcitement : LOOP
SET count = count+1;
IF count<sums then
SELECT FLOOR(1 + (rand()*5)) INTO bookRating;
IF bookRating <3 then
SET bookReview = 'This book isn't good';
ELSEIF bookRating <=4 OR bookRating >=3 then
SET bookReview = 'This book is good';
ELSE
SET bookReview = 'This book is very good, you should read it';
END IF;
UPDATE Book SET Rating = bookRating AND Review = bookReview
WHERE idBuku=count;
END IF;
END LOOP getBookExcitement;
END
but when i try to call it, my workbench just keep executing the query non stop. Just like it stuck in loop. but the way i see it, i cant find something wrong with that looping, please help me since im new with stored procedure in mysql.
thanks
There's no LEAVE getBookExcitement;
Put an else at the end of the IF count<sums then block, with a LEAVE statement.
I have a situation in which I would like to iterate through a "schedPayments" table that stores a schedule of payments corresponding with a client in the "client" table. The client table also contains a "status" column at the moment holds a 0 for "Past Due" and a 1 for "Current". When the balance from the client table is greater than the supposed balance from the schedPayments table AND today's date is later than the date the payment was scheduled for, the status column in the clients table should be set to 0.
I may be completely off the wall with my solution, but I keep getting Error Code: 1329. No Data - zero rows fetched, selected or processed. The MySQL Workbench lacks some major debugging capabilities that I wish it had. The documentation also doesn't quite cover what I need either in this situation.
CREATE PROCEDURE `project`.`status_update` ()
BEGIN
DECLARE balance DECIMAL(20) DEFAULT 0;
DECLARE cID INT(10) DEFAULT 0;
DECLARE currentID INT(10) DEFAULT 0;
DECLARE supposedBal DECIMAL(20) DEFAULT 0;
DECLARE payDate DATE;
DECLARE cur1 CURSOR FOR SELECT ClientID,SupposedBalance,Date FROM project.schedpayments;
OPEN cur1;
status_loop: LOOP
FETCH cur1 INTO cID, supposedBal, payDate;
BLOCK2: BEGIN
DECLARE cur2 CURSOR FOR SELECT balance FROM project.client WHERE ID=cID;
OPEN cur2;
FETCH cur2 INTO balance;
IF currentID > cID THEN
SET currentID = cID;
IF (CURDATE() > payDate) AND (supposedBal < balance) THEN
UPDATE feeagree SET Status=0 WHERE ID=cID;
END IF;
CLOSE Cur2;
END IF;
END BLOCK2;
END LOOP;
CLOSE cur1;
END $$
You can see the remnants of how I had enclosed the entire procedure in a block and that only resulted in the compiler thinking the first block ended with END BLOCK2; and that resulted in an Error Code 1325. Cursor is already open.
I am definitely making this more complicated than necessary, so any help would be much appreciated. The only way I learn this stuff is trial by fire, and it is super hot today.
It seems that you don't need all those cursors and you can achieve your goal with one UPDATE statement.
It's hard to be precise without seeing your tables structures and sample data, but a more succinct version of your SP might look like this
CREATE PROCEDURE status_update()
UPDATE feeagree
SET Status = 0
WHERE ID IN
(
SELECT p.cID
FROM schedpayments p JOIN client c
ON p.cID = p.ID
WHERE p.Date < CURDATE()
AND p.SupposedBalance < c.balance
GROUP BY p.cID
);
...
DECLARE done TINYINT DEFAULT FALSE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET DONE = true;
OPEN cur1;
status_loop: LOOP
FETCH cur1 INTO cID, supposedBal, payDate;
IF DONE = true THEN LEAVE status_loop; END IF;
...
SET DONE = false;
END LOOP;
The SET DONE = false at the end resets DONE in case anything in the inner block results in it getting set to TRUE;