Solved

Stored procedure passing multiple input values to a variable in select statement

Posted on 2009-04-07
8
1,124 Views
Last Modified: 2012-05-06
Hello,
I have a stored procedure that prompts for a user input value as a variable which is then passed to a select statement. It works fine but only for a single value entered.  I would like to be able to offer the user to input multiple values into a single variable if possible. Example: A user would enter CHID as 1,2,3,5 as @chid and the select statement would be select * from db1 where CHID IN (@CHID): for all records that values of 1,2,3,5. Thanks. c
Procedure:

ALTER procedure [dbo].[sp_Master_1]
@CHID varchar(2)  -user enters 1,2,3,5
as
SELECT a, b, c
INTO [dbo].[A]
WHERE CHID IN ('1','2','3','5')
0
Comment
Question by:cbads
  • 4
  • 3
8 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 24088732
please find my procedure dbo.ParmsToList() on EE, and you can use it like this:
ALTER procedure [dbo].[sp_Master_1]
@CHID varchar(2000)  ---user enters 1,2,3,5
as
SELECT a, b, c
INTO [dbo].[A]
WHERE CHID IN (select value from dbo.ParmsTolist(@CHID, ',') )

Open in new window

0
 

Author Comment

by:cbads
ID: 24088991
Thanks. I created the function for dbo.ParmsToList() and added the (select value from dbo.ParmsTolist(@CHID, ',') )
to my stored procedure but I get this message when I save it:

Msg 8144, Level 16, State 3, Procedure sp_Master_1, Line 117
Procedure or function dbo.ParmsTolist has too many arguments specified.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24089214
you got a version without the second argument.


ALTER procedure [dbo].[sp_Master_1]
@CHID varchar(2000)  ---user enters 1,2,3,5
as
SELECT a, b, c
INTO [dbo].[A]
WHERE CHID IN (select value from dbo.ParmsTolist(@CHID) )

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:cbads
ID: 24089759
Here's the link to the dbo.ParmsToList() I'm using...is this the wrong version?

http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_21627393.html

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24090710
there are 2 versions, the one you copied, and one (in the "open discussion") contains the additional parameter version.

for your needs, you don't have to use that version  :)
0
 

Author Comment

by:cbads
ID: 24091254
Thank You. I used the one in the open discussion and it's working perfectly.
cb
0
 

Expert Comment

by:Lorna70
ID: 33936389
Sorry being a SQL newbie, I'm really struggling to get my head round this.  Thought I'd go for the simplest:

UPDATE tLoads
SET RouteID = @routeID
WHERE LoadID  IN
  (SELECT     Value
   FROM   dbo.ParmsToList(@loadIDList, ',') AS ParmsToList_1))

However, I don't know where ParmsToList comes from and this @loadIDList - is this an array and if so, how do I declare it in SQL?  
If someone could give me advice about learning this stuff, that would be great too.
Many thanks
Lorna
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33936436
dbo.ParmsToList can be found here:
http://www.experts-exchange.com/A_1536.html
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 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