简体   繁体   中英

Stored procedure - Multiple string parameters with IN clause sql

i want to filter the rows using Stored procedure. below is my query

   Create PROCEDURE [dbo].[Test]      
   @SearchTerm VARCHAR(100)        
   AS      
   BEGIN     

        Select * from master where name in (@SearchTerm)

   END

In code behind,

    cmd.Parameters.AddWithValue("@SearchTerm", "peter")

when i run with above parameter, it's work fine.

but when i pass like this cmd.Parameters.AddWithValue("@SearchTerm", "'peter','rahul'")

this time no rows fetching.

i tried manually then also it's not working.

    exec Test ''peter','rahul''

Please help me, how to pass muliple string Using IN clause?

One method is

Create PROCEDURE [dbo].[Test]      
   @SearchTerm VARCHAR(100)        
   AS      
    BEGIN     
      Select * from master where ','+@SearchTerm+',' like '%,'+name+',%'

You can find more methods at http://www.sommarskog.se/arrays-in-sql-2008.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM