Solved

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

Posted on 2009-04-07
8
1,119 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

22 Experts available now in Live!

Get 1:1 Help Now