postgresql7.1 select count(*) as duplicate where exists (select option_id from ttracker_options where package_id = :package_id and function = :function and (value = :value or upper(name) = upper(:name))) select ttracker_option__add_option ( null, -- option_id :package_id, -- package_id :function, -- function :name, -- name :value -- value )