working stored procedure conversion from pljava