DELIMITER $$

USE `apzl`$$

DROP FUNCTION IF EXISTS `getOwnName`$$

CREATE DEFINER=`apzl`@`%` FUNCTION `getOwnName`(pUserID VARCHAR(40)) RETURNS VARCHAR(80) CHARSET gbk 
BEGIN

    DECLARE result VARCHAR(80);

    IF LEFT(pUserID,4)='DSTB' THEN
    SELECT distributor_name INTO result FROM distributor_info WHERE distributor_no=pUserID;
    ELSE
    SELECT customername INTO result FROM customer_info WHERE customerid=pUserID;
    END IF;
    RETURN result;
    
    END$$

DELIMITER ;