OK, I tried making a procedure and declaring the variable outside of the UPDATE statement, but I'm still getting syntax errors related to declaring and setting the variable. Here is my code:
DELIMITER $$
DROP PROCEDURE IF EXISTS `procA`.`tableA`$$
CREATE PROCEDURE `procA`.`tableA`()
BEGIN
DECLARE cnt int
Update tableA
Set visit_type =
SET cnt = 1
WHILE (cnt < 5) -- largest country code is 4 digits
BEGIN
case when (count(select location from main where SUBSTRING(pid,4,cnt)) = 1) then
(select location from main where SUBSTRING(pid,4,cnt))
cnt = 5
else
cnt = cnt + 1
END
END
END
where isnull(visit_type);
END$$
DELIMITER ;