0

Possible Duplicate:
Parameterizing an SQL IN clause?
SQL Server SP - Pass parameter for “IN” array list?

In my SP, I am taking string of data from Reference table to get the SQL select filtered.

here is scenario,

Regionids = 2,6,8 I get this value to a string @regions

In my SP i do calculations and atlast have a big SQL to return the data. Based on Calculations, i get date and ids from which to get records. Also I want to restrict rows to above region specified in Reference.

So in my Where clause of the SELECT, AND RG.regionids IN ( @regions )

I get error, Conversion failed when converting the varchar value '2,6,8' to data type int.

How can i accomplish above request?

1

1 Answer 1

1

The easiest way would be to use dynamic SQL:

EXECUTE('SELECT * 
         FROM   Region R 
         WHERE  R.RegionID IN (' + @regions + ')')
Sign up to request clarification or add additional context in comments.

1 Comment

Just be aware that it is open to and SQL injection attack, especially if the @regions variable is user supplied.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.