Solved

Passing comma separated list to a stored procedure

Posted on 2006-10-19
2
961 Views
Last Modified: 2007-12-19
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
Comment
Question by:YZlat
  • 2
2 Comments
 
LVL 14

Expert Comment

by:Thandava Vallepalli
ID: 17769375
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
 
LVL 14

Accepted Solution

by:
Thandava Vallepalli earned 125 total points
ID: 17769429
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now