Procedure with input validation
SQL
Medium
3 views
Problem Description
Create sp_add_coupon(code, discount) that validates discount first. If discount is not in 1..80, raise an error; otherwise insert the coupon.
Input Format
SQL procedure
Output Format
Procedure created
Sample Test Case
Input:
Coupons(code, discount_percent)
Output:
Procedure created
Constraints
MySQL-style SIGNAL for error
Official Solution
CREATE PROCEDURE sp_add_coupon(IN p_code VARCHAR(50), IN p_disc INT) BEGIN IF p_disc < 1 OR p_disc > 80 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid discount'; ELSE INSERT INTO Coupons(code, discount_percent) VALUES (p_code, p_disc); END IF; END;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!