troubleshooting Question

Stored Procedure help

Avatar of Zolf
ZolfFlag for United Arab Emirates asked on
SQLMicrosoft SQL ServerMySQL Server
7 Comments3 Solutions26 ViewsLast Modified:
Hi there,

I have this SP and want to understand the logic as to how it is working. i will appreciate if someone can explain it.

CREATE PROCEDURE updeditQuantity(p_qty int, p_contractmfgid int, p_rawmaterialid int)
BEGIN
DECLARE c_id, c_qty INT;
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR
  SELECT id, quantity 
    FROM inventory_rawmaterial_contractmfg
   WHERE contractmfgid =p_contractmfgid AND rawmaterialid=p_rawmaterialid
   ORDER BY quantity;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN cur1;

read_loop: LOOP
FETCH cur1 INTO c_id, c_qty;

IF done or (p_qty = 0) THEN
  LEAVE read_loop;
END IF;
    
IF (p_qty >= c_qty) then
  set p_qty = p_qty - c_qty;
  update inventory_rawmaterial_contractmfg set quantity=0,dateupdated=now() where id=c_id;
ELSE
  update inventory_rawmaterial_contractmfg set quantity=quantity+p_qty,dateupdated=now() where id=c_id;
  set p_qty = 0;
END IF;

END LOOP;

END

Open in new window

ASKER CERTIFIED SOLUTION
ste5an
Senior Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 3 Answers and 7 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 3 Answers and 7 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004