Skip to content

END

CREATE PROCEDURE productpricing()
BEGIN
DECLARE p_price INT;
DECLARE p_name VARCHAR(20);
DECLARE cur1 CURSOR FOR SELECT name, price FROM products;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO p_name, p_price;
IF p_price > 100 THEN
SELECT p_name, 'High' as price_category;
ELSE
SELECT p_name, 'Low' as price_category;
END IF;
IF done THEN LEAVE read_loop; END IF;
END LOOP;
CLOSE cur1;
END
// executing CALL productpricing();
'Product1', 'High'
'Product2', 'Low'
'Product3', 'High'
'Product4', 'Low'

The given SQL code demonstrates a stored procedure in MySQL named productpricing. This procedure uses a cursor to loop through all products in the products table. Using the IF statement, it categorizes each product as ‘High’ if its price is greater than 100, or ‘Low’ otherwise. The END IF marks the end of the IF control structure, whereas END LOOP indicates the end of the LOOP structure. Lastly, END signifies the end of the procedure.

The output displays the names and price categories of each product after calling the productpricing procedure.