Conditional Join Statements based on Variable - SQL Server -


i have question altering statements in clause based on variable flag passed in ui.

in scenario there person records in db , goal allow user find duplicates based on criteria select. permutation of search options considered duplicates.

the query simplified example, concept same. able included conditionally in join using if or case, or each permutation need own separate query?

declare @lastnamedup varchar(1) = 'n',         @firstnamedup varchar(1) = 'n',         @dobdup varchar(1) = 'n',         @ssndup = 'n',         @zipdup= 'n'  /*ui values passed set y*/  select    p.id    p.lastname,    p.firstname,    p.ssn,    p.dob,    p.address1,    p.address2,    p.zip    p.phone person p join person p2 on p.id <> p2id                , /* conditionally join based on dup flags activated                1 or many of of 5 dup variables example                @lastnamedup = 'y' include , p.lastname = p2.lastname*/ 

what add joins or statement in check value of parameter still end join. build sql command in nvarchar variable (@sql) adding joins needed , perform execute(@sql) results complete query.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -