Results 1 to 3 of 3

Thread: Using 'IN' query in Stored Procedure

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Reading
    Posts
    70

    Using 'IN' query in Stored Procedure

    I'm trying to develop a sproc that does the following type of search.


    Select * from table1 where id in (101, 102, 103)

    Obviously I don't want to hardcode values in so I would rather have

    create procedure GetListofValues
    (@ID varchar(500))
    as
    select * from table1 where id in (@ID)


    but of course I can't use this because the column I'm searching on is an integer columns (it's an identity column so I can't change it) and I can't pass it in as an integer because of the comma separation of various different numbers.

    Does anyone have any ideas?

    Cheers.

    AuldNick

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    You need to create/execute a dynamic sql string.

    Code:
    create procedure GetListofValues
    (@ID varchar(500)) 
    as
    declare @SQL varchar(2000)
    
    Set @SQL = 'select * from table1 where id in (' + @ID + ')'
    
    exec sp_executesql @sql

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Reading
    Posts
    70
    That's exactly what I needed. Thanks Brucevde.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width