Parameter for IN clause

  • I have a s/p along the lines of:

    select * from mytable where id in(@ids)

    id is an int and @ids is defined as varchar(255)

    I pass in say '1,2,3' as the param and get a 'syntax error converting the varchar value '1,2,3' to a column of data type int.'

    How should I do this?

  • You'll need to split the values into separate values, or use dynamic SQL to build your Where clause.

    The article Ninja linked to is on how to split the values from a comma-delimited list into rows of numbers.

    The other common option is dynamic SQL. That has the usual security issues, so I recommend the string split option (Ninja's link).

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

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

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