sql server - T-SQL Find matching rows based on BIT parameter values -


i trying create stored procedure returns rows within person table have same value particular columns. want stored procedure accept parameter values such @match_on_forenames bit , @match_on_surname bit. example exec find_matching @match_on_forenames 1, @match_on_surname 1 find rows in table forenames , surname fields match, not matching against itself.

i have stored procedure similar :

alter proc get_matching_records     @match_on_forenames bit,     @match_on_surname bit select distinct     p.forenames, p.surname      person p            exists        (             select                  p1.forenames, p1.surname                              person p1                                                                                                                                                                                                                            (((@match_on_forenames = 1 , p.forenames = p1.forenames , p.id <> p1.id)) or @match_on_forenames = 0)                 , (((@match_on_surname = 1 , p.surname = p1.surname , p.id <> p1.id)) or @match_on_surname = 0)                 ) 

soon introduce or @match_on_forenames performance of query becomes poor.

any ideas how best approach problem?

many thanks.

this logic:

where exists (select 1               person p1                                                                                                                                                                                                                              (((@match_on_forenames = 1 , p.forenames = p1.forenames , p.id <> p1.id)) or @match_on_forenames = 0)                 , (((@match_on_surname = 1 , p.surname = p1.surname , p.id <> p1.id)) or @match_on_surname = 0)              ) 

if fast without or, 3 conditions might solve problem:

where (@match_on_forenames = 1 , @match_on_surname = 1 ,        exists (select 1                person p1                                                                                   (p.forenames = p1.forenames , p.id <> p1.id) ,                      (p.surname = p1.surname , p.id <> p1.id)               )       ) or       (@match_on_forenames = 0 , @match_on_surname = 1 ,        exists (select 1                person p1                                                                                   (p.surname = p1.surname , p.id <> p1.id)               )       ) or       (@match_on_forenames = 1 , @match_on_surname = 0 ,        exists (select 1                person p1                                                                                   (p.forenames = p1.forenames , p.id <> p1.id)                )       ) 

each of exists clauses should fast, based on observation simpler query faster.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -