Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 IN() statement

Author  Topic 

ElTerrible35
Starting Member

3 Posts

Posted - 2009-03-16 : 10:35:11
I am new to SQL and this forum, but I need some help (I don't even know if what I'm going to ask applies to this section so bear with me...

I need to pass a criteria list into a stored procedure for a specific set of values to be selected within a certain column. For example, I have a column named 'AGE' which contains an integer datatype. I want to use the stored procedure like this:

EXEC spStoredProcedure @AgeCriteria="25,30,35"
Such that the only records that are chosen have ages of 25, 30, or 40.

I have declared the @AgeCriteria as VarChar(100) and written the line:
where TABLE1.Age In (@AgeCriteria)

The error I get says cannot convert my criteria list to integer. How do I handle this?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-03-16 : 10:58:32
See http://www.sommarskog.se/arrays-in-sql-2005.html



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-16 : 11:17:53
quote:
Originally posted by ElTerrible35

I am new to SQL and this forum, but I need some help (I don't even know if what I'm going to ask applies to this section so bear with me...

I need to pass a criteria list into a stored procedure for a specific set of values to be selected within a certain column. For example, I have a column named 'AGE' which contains an integer datatype. I want to use the stored procedure like this:

EXEC spStoredProcedure @AgeCriteria="25,30,35"
Such that the only records that are chosen have ages of 25, 30, or 40.

I have declared the @AgeCriteria as VarChar(100) and written the line:
where TABLE1.Age In (@AgeCriteria)

The error I get says cannot convert my criteria list to integer. How do I handle this?



use where condition as

where ',' + @AgeCriteria + ',' LIKE '%,' + CAST(TABLE1.Age as varchar(3)) + ',%'
Go to Top of Page

ElTerrible35
Starting Member

3 Posts

Posted - 2009-03-16 : 11:34:28
visakh16, that worked perfectly. Thanks for the quick response. Peso, I was planning to read that article at a later time. Thanks for both of your help.
Go to Top of Page
   

- Advertisement -