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
Post a Comment