I'm trying to use a case statement to update a column based on the value of another column. My table called 'rma' in MySQL is as follows:
ID | rma_number | serial_number | ref_status_id
1 | 9898 | 123456 | 19
2 | 7869 | 098768 | 3
Here is my stored procedure:
CREATE DEFINER=`admin`@`localhost` PROCEDURE `delete_update_rma`(in selectedID int, in selectedRMAID int)
begin
declare rmaStatus int(5);
select ref_status_id into rmaStatus
from rma
where id = selectedRMAID;
case rmaStatus
when 19 then
set ref_status_id = 0;
end case;
delete from dropbox where id = selectedID;
end
When I try to save to create the procedure I receive the error #1193 - Unknown system variable 'ref_status_id'.
Can anybody help me identify what may be wrong with my stored procedure?
Try this:
CREATE DEFINER=`admin`@`localhost` PROCEDURE `delete_update_rma`(IN selectedID INT, IN selectedRMAID INT)
BEGIN
UPDATE rma
SET ref_status_id =
CASE ref_status_id
WHEN 19 THEN 0
WHEN 3 THEN 2
ELSE ref_status_id
END
WHERE id = selectedRMAID;
DELETE FROM dropbox WHERE id = selectedID;
END
Check the SQL FIDDLE DEMO
OUTPUT
| ID | RMA_NUMBER | SERIAL_NUMBER | REF_STATUS_ID |
|----|------------|---------------|---------------|
| 1 | 12345 | 67898 | 0 |
| 2 | 45645 | 89056 | 2 |