Need help in dynamic query with IN Clause

前端 未结 3 876
爱一瞬间的悲伤
爱一瞬间的悲伤 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:47

    If your verdor name is

     declare @in varchar(100)
     select @in = 'HP,LENOVO'
    

    You can use dynamic SQL

     declare @sql nvarchar(1000)
     select @sql = 'select * from yourtable where yourfield in ('+@in +')'
     exec sp_executesql @sql
    

    or you can make your split function return a table

     select * 
     from yourtable
         inner join dbo.f_Split(@in) f 
         on yourtable.yourfield =f.entry
    

    The second is much preferable due to its protection from SQL injection type attacks

提交回复
热议问题