Solved

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

Posted on 2009-04-07
8
1,128 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 143

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 143

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
Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

 

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 143

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 143

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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 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