รายละเอียดของการตอบ ::
DELIMITER $$
CREATE FUNCTION sort_num_3 (num char(4))
RETURNS char(4) DETERMINISTIC
BEGIN
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE v_num varchar(4) DEFAULT "";
DECLARE rs_num varchar(4) DEFAULT "";
DEClARE num_cursor CURSOR FOR
SELECT A FROM (
SELECT LEFT(num,1) A union all
SELECT SUBSTRING(num,2,1) union all
SELECT SUBSTRING(num,3,1) union all
SELECT RIGHT(num,1)
) T ORDER BY A;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished = 1;
OPEN num_cursor;
get_num: LOOP
FETCH num_cursor INTO v_num;
IF v_finished = 1 THEN
LEAVE get_num;
END IF;
SET rs_num = CONCAT(rs_num, v_num );
END LOOP get_num;
CLOSE num_cursor;
RETURN rs_num;
END$$
DELIMITER ;