Check the query, just read ONE REGISTER to return the MAX each year !!!. select first 1 n_orden + 1 from ordenes where year_orden = new.year_orden order by year_orden desc, n_orden desc into new.n_orden; /*-----------------------------------------------------------*/
The query that you use does the same, returns the MAX, but read all records !!!. select coalesce(max(o.n_orden),0)+1 from ordenes o where o.year_orden=new.year_orden into new.n_orden; /*-----------------------------------------------------------*/ I thought that was the problem, read all the records for the MAX. Regards. Christian [Non-text portions of this message have been removed]