Need help in dynamic query with IN Clause

前端 未结 3 874
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 07:17

I have 1 Table which is named as ProductMaster.Another Table which is VendorMaster. Now i want to find all the products from specified Vendormaster.

So i want to use

3条回答
  •  死守一世寂寞
    2020-12-12 07:49

    You are better off using a join and nullable parameters individually

    consider this table1 id joins on table 2 id, and the where clause can use paramter p1 for col1 or p2 for col2 etc...

    select * 
    from table1 t1
    inner join table2 t2 on t2.id = t1.id -- join the tables
    where 1=1
    and t2.col1 = isnull(p1, t2.col1)
    and t2.col2 = isnull(p2, t2.col2)
    .
    . -- etc...
    .
    

    If the p1 is null then you get a test of t2.col1 = t2.col1 which effectively dismisses p1 from the where clause.

提交回复
热议问题