DB optimized and fixed errors for stored procedures