c# - How to do Searching and Filtering Data based on Checkboxes in Many to Many Relationship in SQL? -
basically have 3 tables (with many many relationship); , querying searching this;
alter proc [dbo].[usp_contactsearch] ( @personname varchar(60)= '', @mobileno varchar(20)= '', @nationlity varchar(50)='' , @contacttypes varchar(max) = '' ) begin select distinct c.contactid, c.personname, ct.contacttype, ct.contacttypeid contact c left outer join contactwithcontacttype cct on c.contactid = cct.contactid left outer join contacttype ct on cct.countacttypeid = ct.contacttypeid c.personname case when @personname='' c.personname else '%'+@personname+'%' end , c.mobileno1 case when @mobileno='' c.mobileno1 else '%'+@mobileno+'%' end , c.nationality case when @nationlity='' c.nationality else '%'+@nationlity+'%' end end
so, result data default is;
so, front end, have contacttypes (which dynamic i.e comming contact types table), , interface looks this
now, whenever user check propertyowner(contacttypeid=1), data should filtered , contacts should shown belong contacttypeid=1 silarly, when check second checkbox i.e tenant(contacttypeid=2). data should more filtered , contacts displayed belongs contacttypeid= 1 , 2. , 3rd contacttype, data should more filtered , on , forth.
so, problem contacttypes dynamic , don't know how handle situation.
regards query , performance apreciated.
try this. work...
-- user defined table type variable declare @mytypedatatype conttype -- pass value variable front end insert @mytypedatatype values(1),(2),(3); -- front end pass -- selected values "table type variable" , -- "varchar" variable declare @type varchar(20); set @type = '1,2,3'; select x.* ( -- query persons, -- have 1 type u want search... select c.*,ctt.contacttype, ctt.contacttypeid contact c inner join contactwithtype ct on c.contactid = ct.contactid inner join contacttype ctt on ctt.contacttypeid = ct.contacttypeid @type '%' + cast( ct.contacttypeid varchar(max)) + '%' ) x inner join ( -- count record of each person, -- how many time persons record exists.. select c.contactid, count(c.contactid) total contact c inner join contactwithtype ct on c.contactid = ct.contactid inner join contacttype ctt on ctt.contacttypeid = ct.contacttypeid @type '%' + cast( ct.contacttypeid varchar(max)) + '%' group c.contactid )y on x.contactid = y.contactid -- filter persons , y.total = (select count(*) @mytypedatatype)
Comments
Post a Comment