postgresql7.1 select attr_id from (select attr_id, sort_key from ab_contact_attrs where sort_key $operator (select sort_key from ab_contact_attrs where attr_id = :attr_id) and contact_id = :contact_id) order by sort_key $direction limit 1 select ab_contact_attr__swap_sort(:attr_id, :other_attr_id);