-- find as much info as you can about a caller
CREATE or REPLACE FUNCTION getcallerinfo(integer, varchar) RETURNS VARCHAR AS '
   DECLARE
     _name VARCHAR;
     _contact1 VARCHAR;
     _contact2 VARCHAR;
     _contact3 VARCHAR;
     _customerid VARCHAR;
   BEGIN
     select into _name, _contact1, _contact2, _contact3, _customerid name, contact1, contact2, contact3, customerid
       from  fastdial_company where company = $1 and
             (number = $2 OR $2 like number OR  number = (''00'' || $2) OR (''00'' || $2) like number)
       order by number desc limit 1;
     IF NOT FOUND THEN
       return ''####'';
     END IF;
     return _name || ''#'' || _contact1  || ''#'' || _contact2  || ''#'' || _contact3  || ''#'' || _customerid;
   END;
' LANGUAGE plpgsql;

