• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 969
  • Last Modified:

Passing comma separated list to a stored procedure

I have a stored procedure that takes a list of values as an input argument. The list is in the format:

'DFG','ABC','ASD','FGH'

I want the result to be

SELECT field1, field2, field3 FROM Table1
WHERE UPPER(RIGHT(RTRIM(field1),3)) IN ('DFG','ABC','ASD','FGH')

here is my stored procedure. What am I doing wrong here?

CREATE PROCEDURE dbo.sp1
@list as varchar(200)
 AS

SELECT field1, field2, field3 FROM Table1
WHERE UPPER(RIGHT(RTRIM(field1),3)) IN ( ' ' + @list + ' ' )
0
YZlat
Asked:
YZlat
  • 2
1 Solution
 
Thandava VallepalliCommented:
try this one

CREATE PROCEDURE dbo.sp1
@list as varchar(200)
 AS

exec ( 'SELECT field1, field2, field3 FROM Table1 WHERE UPPER(RIGHT(RTRIM(field1),3)) IN ( ' + @list + ' ) '


itsvtk
0
 
Thandava VallepalliCommented:
Ooops...!  forgot the ')' at end....

try this one

=======================
CREATE PROCEDURE dbo.sp1
@list as varchar(200)
 AS

exec ( 'SELECT field1, field2, field3 FROM Table1 WHERE UPPER(RIGHT(RTRIM(field1),3)) IN ( ' + @list + ' ) ' )
=========================



and you need to call this proc as below

exec sp1 @list = '''DFG'',''ABC'',''ASD'',''FGH'''



itsvtk
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now