list of values in input parameter

  • Hello,

    I have the following issue:

    I have the following statement into a function:

    select a,b form T where c IN @parameter

    t is the table

    c is datatype= integer

    @parameter is a input parameter in the function, the @parameter contains more values and passed as a string.

    Running the statement above I got error due to conversion type.

    How can I pass a list of parameters in the @parameter variable to make the statement works?

    Thank in advance.

  • I assume @parameter is a comma-separated list, or something similar? If that's the case, see Jeff Moden's article on "Numbers" or "Tally" tables for a solution to this.

    http://www.sqlservercentral.com/articles/TSQL/62867/

    John

  • Hello,

    thank for your help,

    besides in internet I have found out this useful code:

    declare @MyStr varchar(500)

    set @MyStr = 'Value 1, Value 2, Value 3'

    DECLARE @x XML

    SET @x = ' '

    select a,b

    from T

    where c IN (

    --change the data type to the correct one below

    SELECT x.i.value('.', 'VARCHAR(10)') as [MyID]

    FROM @x.nodes('//i') x(i)

    )

    Or, using inner join:

    declare @MyStr varchar(500)

    set @MyStr = 'Value 1, Value 2, Value 3'

    DECLARE @x XML

    SET @x = ' '

    select a,b

    from T

    inner join(

    --change the data type to the correct one below

    SELECT x.i.value('.', 'VARCHAR(10)') as [MyID]

    FROM @x.nodes('//i') x(i)

    ) as List

    on t.C = List.MyID

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

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