Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-04-07
8
Medium Priority
?
1,131 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

722 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