How to use multiple values in a single query parameter

  • Sorry if this is an obvious question, it's not to me.

    I have a number of object identifiers (ints) which I want to use in a query

    WHERE

    (Id IN ('1', '2', '3'))

    How can I get multiple values into a query using a parameter(s) ?

    Thanks

    Mark

  • OK, you might have guessed that I am new to this.

    Here is something I tried, it doesn't work but might illustrate what I am trying to do:

    declare @ids varchar

    SET @ids = '(1,2)'

    SELECT

    SATA_AllocationObject.*, Id AS Expr1

    FROM

    SATA_AllocationObject

    WHERE

    (Id IN @ids)

    Thanks

    Mark

  • declare @ids varchar(5)

    SET @ids = '1,2'

    declare @sql varchar(1000)

    select @sql = 'SELECT SATA_AllocationObject.*, Id AS Expr1'

    select @sql = @sql + ' FROM SATA_AllocationObject'

    select @sql = @sql + ' WHERE Id IN (' + @ids + ')'

    exec (@sql)

     

    But use only if you are sure you are not subject to SQL injection.


    N 56°04'39.16"
    E 12°55'05.25"

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply